Checking a date is in the future

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Checking a date is in the future

Post by MathewByrne »

I have 3 text boxes, day, month and year so I end up with somthing like

Code: Select all

/**
 * Day = 1-31
 * Month = 1-12
 * Year = 2005-whenever
 */

$day = $_POST["day"];
$month = $_POST["month"];
$year = $_POST["year"];
I want to check that the date is not only valid (via checkdate()) but also that it is in the future.

Also is there a simple way to add an integer amount of year (1, 2, 3... etc) to this date? For example if I added 1 to 1 Jan 2006 I'd get 1 Jan 2007.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

You can use mktime() to add and subtract dates. To figure out if a date is greater than the current convert incoming string dates to unix time and compare it to time(). strtotime() is a great way to convert so is mktime().
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post by MathewByrne »

Thanks, done the trick nicely
Post Reply