Compare given date with today


I have following

$var = "2010-01-21 00:00:00.0"

I'd like to compare this date against today's date (i.e. I'd like to know if this $var is before today or equals today or not)

What function would I need to use?

9/14/2015 5:34:27 PM

Accepted Answer


Turns it into a time value

time() - strtotime($var);

Gives you the seconds since $var

if((time()-(60*60*24)) < strtotime($var))

Will check if $var has been within the last day.

1/21/2010 11:59:54 PM

That format is perfectly appropriate for a standard string comparison e.g.

if ($date1 > $date2){

To get today's date in that format, simply use: date("Y-m-d H:i:s").


$today = date("Y-m-d H:i:s");
$date = "2010-01-21 00:00:00";

if ($date < $today) {}

That's the beauty of that format: it orders nicely. Of course, that may be less efficient, depending on your exact circumstances, but it might also be a whole lot more convenient and lead to more maintainable code - we'd need to know more to truly make that judgement call.

For the correct timezone, you can use, for example,


Click here to refer to the available PHP Timezones.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow