Forum Moderators: coopster

Message Too Old, No Replies

Date Difference for Form Validation php

         

ukgimp

1:36 pm on May 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have two dates from a form and intend to insert them into a MySQL (mm.dd.yyyy) db which is in a date format. I have each aspect in dropdeon (dd, mm, yyyy) I would like to validate that the second date is >= the first. I could explode the two dates and do a set of if’s which seems very complicated.

Is there a date function to do this? I have looked but cannot find a bit of code to adapt.

Regards

ukgimp

2:21 pm on May 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this should work:

$aDay ="11";
$aMonth ="05";
$aYear = "2003";
$unixtime = mktime(0,0,0,$aMonth,$aDay,$aYear);
echo $unixtime;

then subtract the second one from the first, if positive you have a muppet filling in your form.

If there is better way I would love to know.

Cheers

daisho

2:28 pm on May 16, 2003 (gmt 0)

10+ Year Member



mySQL dates are in the format "YYYY-MM-DD" not "mm.dd.yyyy". MySQL will yell at you if you don't sent the correct format.

I'll take it that you can build that date. Lets say that $date1 is the first date and $date2 is the second date.

$result=strcmp($date1,$date2);

strcmp is a string comparison function but will work fine in this case.

if $result is 0 then $date1 and $date2 are the same.
if $result is < 0 $date1 is less than $date2
if $result is > 0 $date1 is greater than $date2

This should give you the test you need.

daisho.