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. */
$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.
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.