Form Date Validation

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
rlwillis
Forum Newbie
Posts: 2
Joined: Wed May 11, 2005 8:50 am

Form Date Validation

Post by rlwillis »

Hi Folks, I have an order form with a "product delivery date" entry. The delivery date request must be at least 30 days from the current date. How would I validate this?

Thanks for any consideration.

rlwillis
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You need to get the date they input into US style date format mm/dd/YYYY and then you can do some calculations by converting it to a timestamp using strtotime().

Code: Select all

//Example input date
$in_date = '05/25/2005'; //25th May 2005

$in_stamp = strtotime($in_date); //Unix timestamp of 25th May 2005 in seconds

$now = time(); //The unix timestamp now

if ($in_date - $now > (60 * 60 * 24 * 30)) { //secs * mins * hours * days
    echo 'Date is more than 30 days from now';
} else {
    echo  'Date is within 30 days of now';
}
rlwillis
Forum Newbie
Posts: 2
Joined: Wed May 11, 2005 8:50 am

Post by rlwillis »

Thanks!
Post Reply