WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : How To Get a Date In The Next 60 Days
Categories : PHP, Date Time Click here to Update Your Picture
Joshua Walcher
Date : Jun 13th 2008
Grade : 3 of 5 (graded 4 times)
Viewed : 714
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Joshua Walcher
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

You can see this example at: http://www.pkob.info/dateplus/ (and good luck on the dates).

I needed a date range tool for a stock portfolio game I'm building. Basically, the way it works is that users pick a stock, a direction (short,long), a price, and a length of time that is between 5 and 60 days. Everything is easy to process except for the date. I needed to be able to compare the future date to the current date to see if the user's pick has expired or not. Here's what I came up with...

First we need a form to send to the processing page:

--- index.php ---
<html>
<head>
<title>Josh's Date Tool</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="dateplus.php">
  <p>How many days would you like to add to today's date?<br />
    <br />
    <select name="num_days" id="num_days">
      <option value="5">5 Days</option>
      <option value="10">10 Days</option>
      <option value="15">15 Days</option>
      <option value="20">20 Days</option>
      <option value="25">25 Days</option>
      <option value="30">30 Days</option>
      <option value="35">35 Days</option>
      <option value="40">40 Days</option>
      <option value="45">45 Days</option>
      <option value="50">50 Days</option>
      <option value="55">55 Days</option>
      <option value="60">60 Days</option>
    </select>
    <input type="submit" name="button" id="button" value="Submit" />
    <br />
  </p>
</form>
</body>
</html>


That was all HTML, so feel free to name it index.html or .htm if you want.

Now we need to process the form and return a date.

--- dateplus.php ---
<title>Josh's Date Tool - Part Two</title>
<?php
/* Josh's Calc-Script - Used to determine future date that is 1-60 days away from current date. Viewable at http://www.pkob.info/dateplus/ */

$current_year = date(Y); # current year in 4 digit format (i.e. 2004, 2008, 2088, etc.)
$leap_year = "no"; # initialize the leap_year indicator

if($current_year == 2008 || 2012 || 2016 || 2020 || 2024 || 2028 || 2032 || 2036 || 2040 || 2044 || 2048 || 2052 || 2056 || 2060 || 2064 || 2068 || 2072 || 2076 || 2080 || 2084 || 2088 || 2092 || 2096 || 3000 || 3004 || 3008){
 
$leap_year = "yes"; # I figure I won't live past 3008....
}
# Here are the standard number of days in a month.
$jan = 31; $feb = 28; $mar = 31; $apr = 30; $may = 31; $jun = 30; $jul = 31; $aug = 31; $sep = 30; $oct = 31; $nov = 30; $dec = 31;

if(
$leap_year == "yes"){
 
$feb = 29;
}

$current_month = date(m); /* current_month in 2 digit format (01, 10, etc.), but don't worry, you can still perform math on it. */

/* Depending on what month it is, we need to create some variables for the next step.  There's a faster way to do this, but this way is easy to understand. */

if($current_month == 1){$days_in_month = $jan;$next_month_days = $feb; $nmd2 = $mar;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 2){$days_in_month = $feb;$next_month_days = $mar; $nmd2 = $apr;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 3){$days_in_month = $mar;$next_month_days = $apr; $nmd2 = $may;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 4){$days_in_month = $apr;$next_month_days = $may; $nmd2 = $jun;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 5){$days_in_month = $may;$next_month_days = $jun; $nmd2 = $jul;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 6){$days_in_month = $jun;$next_month_days = $jul; $nmd2 = $aug;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 7){$days_in_month = $jul;$next_month_days = $aug; $nmd2 = $sep;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 8){$days_in_month = $aug;$next_month_days = $sep; $nmd2 = $oct;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 9){$days_in_month = $sep;$next_month_days = $oct; $nmd2 = $nov;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 10){$days_in_month = $oct;$next_month_days = $nov; $nmd2 = $dec;$next=$current_month+1;$next2=$current_month+2;}
if(
$current_month == 11){$days_in_month = $nov;$next_month_days = $dec; $nmd2 = $jan;$next=$current_month+1;$next2=1;}
if(
$current_month == 12){$days_in_month = $dec;$next_month_days = $jan; $nmd2 = $feb;$next=1;$next2=2;}

$calc = $_POST['num_days']; //any number between 1 day & 60 days from current date
$current_day = date(d); # today's day of the month in 2 digit format.

$left_this_month = $days_in_month - $current_day;

if(
$calc <= $left_this_month){
$calc_in_month = $calc + $current_day;
 
$future_month = $current_month;
 
$future_day = $calc_in_month;
 
$future_year = $current_year;
}

if(
$calc > $left_this_month){
  if(
$calc <= $left_this_month + $next_month_days){
   
$day_next_month = $next_month_days - ($calc - $left_this_month) ;
   
$day_next_month = $next_month_days - $day_next_month;
    if(
$next == 1){
     
$current_year = $current_year + 1;
    }
     
$future_month = $next;
   
$future_day = $day_next_month;
   
$future_year = $current_year;
  }else{
   
$sofar = $left_this_month + $next_month_days;
   
$day_two_months = $calc - $sofar;
    if(
$next2 == 1 || 2){$current_year = $current_year + 1;}
   
$future_month = $next2;
   
$future_day = $day_two_months;
   
$future_year = $current_year;
  }
}
echo
"The calc returned the date " . $future_month . "/" . $future_day . "/" . $current_year . ".";
?>



A Timing Class
Categories : PHP, PHP Classes, Date Time
Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time
A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Categories : PHP, PHP Classes, Calendar, Date Time
A wrapper function to format dates coming from a databases with the same syntax as PHP's date() function.
Categories : Date Time, Databases, PHP
pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes
PHP Round Clock - Must have Gif support to use this.
Categories : PHP, Date Time, Graphics
A simple and fast calendar combining PHP and tables. Use this as a base for applications in which a calendar is needed.
Categories : Date Time, PHP, Complete Programs, Calendar
Open and Close your website in fixed times .
Categories : PHP, PHP Classes, Cron, Date Time
mysql date/time converters
Categories : PHP, MySQL, Databases, Date Time
Creates three SELECT form fields: Month, Day, and Year. You give it a string which will be used to make the name for the three fields, and a number of seconds to use as the default date. If you give it blank for this value, the current date is used.
Categories : HTML and PHP, PHP, Date Time
If you want to create select buttons featuring current date this example will show you how...
Categories : Date Time, HTML and PHP, PHP
Example of using the pcCalendar class, article 1468 on weberdev.com. Calendar example.
Categories : PHP, Date Time, PHP Classes, Calendar
Monthly and Daily Upcoming Events calendar.
Categories : Date Time, PostgreSQL, PHP, Calendar, Databases
calculus of the eastersunday
Categories : BC math, PHP, Date Time, Databases
Functions used to define a schedule of holidays. Can define non-fixed holidays (eg. 3rd sunday of June).
Categories : Calendar, Date Time, PHP
 Moiz Sitabkhan wrote : 1777
Man.. this is the most weird code! hav'nt u ever heard of mktime()
 
 John Paseur wrote : 1778
Good grief!  You might want to simplify it with something like this:

<?
$future_date 
date'Y-m-d'strtotime('today + ' . {$_POST["num_days"]}) );
?>
 
 Joshua Walcher wrote : 1779
Thanks for the insult, Moiz. Learn how to spell. Yes, I know mktime. 

John, you're absolutely right, it can be simplified, but your suggestion of:

$future_date = date( 'Y-m-d', strtotime('today + ' . {$_POST["num_days"]}) ); 

results in a PHP syntax error. Also, I don't think it returns the right date
even without a syntax error. I agree that this is an ugly way to return a
date, but it does work.
 
 Mark Allsop wrote :1792
<?
$invoice_due_date_mm 
date("m"mktime(0,0,0,date("m")+1,date("d"), date("Y")));
$invoice_due_date_dd date("d"mktime(0,0,0,date("m")+1,date("d"), date("Y")));
$invoice_due_date_year date("Y"mktime(0,0,0,date("m")+1,date("d"), date("Y")));
?>
Append together.
This example does 1 month ahead of the current date. You could do against a user entered date too.
You can check if a yyyymmdd is greater than another yyyymmdd to see if expired.