Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP date comparison

Tags:

date

php

time

How would I check if a date in the format "2008-02-16 12:59:57" is less than 24 hours ago?

like image 239
stef Avatar asked Dec 03 '09 16:12

stef


2 Answers

if (strtotime("2008-02-16 12:59:57") >= time() - 24 * 60 * 60)
{ /*LESS*/ }
like image 144
Don Avatar answered Sep 30 '22 17:09

Don


Just adding another answer, using strtotime's relative dates:

$date = '2008-02-16 12:59:57';
if (strtotime("$date +1 day") <= time()) {
    // Do something
}

I think this makes the code much more readable.

like image 43
LiraNuna Avatar answered Sep 30 '22 16:09

LiraNuna