Date Comparison Does Not Work

Hi all,

I’ve a wierd problem whereby the comparison of 2 dates does not compare accurately.

The signup date(eg. 16-10-2012) is earlier than today’s date(eg. 05-11-2012) and should print “signup date is earlier than today date”. But it keeps going to the else statement printing “signup date is later than today date”.

I don’t see the problem. Can anyone help?




echo $signup = $model->signup_date; //formatted in model such that the date format is 'd-m-Y' (eg. 16-10-2012)

echo "<br />";

echo $today = date('d-m-Y'); //get today date (eg. 05-11-2012)

echo "<br />";

if($signup<$today){

	echo "signup date is earlier than today date";

}else{

	echo "signup date is later than today date";

}



Hi,

i don’t see what is the problem exactly, but if you format fields with ‘Y-m-d’ just for comparison, it could work.

Your dates are of string type. You must use the strtotime() function to convert these strings to time.

Then for example to know if it is earlier or later than today, evaluate the substraction of one from the other. If negative then earlier else later:




if (strtotime($signup)-strtotime($today)<0)

       {echo "signup date is earlier than today date";}

else

       {echo "signup date is later than today date";}



Maybe it must be finetuned in some cases…

Yup. It works now. Tks a lot!