I've written following code to compare a date in a variable with today's date. If the date in a variable is greater than today's date the error message should be echoed. But it's not working properly in my case.
$form_data['trans_date'] = '12-11-2014';//Date to be compared with today's date in mm-dd-yyyy format
if(strtotime(date('m-d-Y')) < strtotime($form_data['trans_date'])) {
echo 'Error';
} else {
echo 'Success';
}
Actually today's date is 12-11-2014 i.e. 11th December 2014 and I'm indirectly comparing today's date with itself. So I should not get an error message but I'm getting the error message.
I tried to debug the code by printing timestamp values then I got following values :
Today's date 1413097200
Trans date 1415779200
Actually since the Trans Date timestamp value is slightly greater than timestamp value of Today's date the error is coming.
But it shouldn't since The date I'm comparing is not greater than today's date.
So how should I resolve this issue?
The values differ not slightly but a month:
Converting your timestamp back to a date yields:
The bug in your code is that strtotime expects either 'd-m-Y' or 'm/d/Y'.