I've following variable containing date in MM-DD-YYYY format. I want to compare this date with today's date. If the date containing in a variable is greater than the today's date I want to echo "Error" and if it is less than or equal to today's date I want to echo "Success". For this thing I don't want to use DateTime class.
I think using UNIX Timestamp values could be a better option. If yo have any other better and efficient option you are welcome.
Following is the variable containing date in MM-D-YYYY format
$form_data['reg_date'] = '12-11-2014'; //This is today's date i.e. 11th December 2014
If the variable $form_data['reg_date']
contains date greater than today's date(i.e. 11th December 2014) it should give error message otherwise should echo success message.
Thanks.
You cannot compare string representation of date in format
m-d-Y
. This format is invalid format, and php will not understand it. Read the manual what date formats are valid.Best way to compare string dates is to have it in format
Y-m-d
or convert your string date to unix timestamp integer. But once you have date inY-m-d
format, it is trivial to convert it to unix timestamp, so converting it to timestamp just for comparing is an unnecessary step.Convert
m-d-Y
tom/d/Y
format, and then to unix timestamp:demo
But this I already explained in answer of your previous/same question (see 2nd link).
Other method could be converting format with
sscanf()
function:demo
But I would still recommend you to use DateTime class, like I already explained if my previous answer.