Joe Torraca
Joe Torraca

Reputation: 2018

PHP is date older than 1 day

I need to check if a date is older than 1 day from a specific time in PHP. I tried the code below but it seems to not work. The code below doesn't seem to work. This leads me to two questions actually.

1) Why does the code below not work?

2) Is this method efficient or should I look into alternative methods?

if (date("Y-m-j H:i:s", strtotime('1 day ago')) < date("Y-m-j H:i:s", strtotime($act['lpwdchange']))) {
    // Code Here
}

Thanks!

Upvotes: 3

Views: 6518

Answers (2)

Scroph
Scroph

Reputation: 51

The easiest way to compare dates will probably be to convert them to timestamps then compare those :

if(strtotime('-1 day') < strtotime($act['lpwdchange']));

The reason why yours doesn't work is probably because it's comparing strings, which might result in undefined behaviour.

Upvotes: 3

Mihai Iorga
Mihai Iorga

Reputation: 39724

use negative ...

strtotime('-1 day')

but if you already have a time stored in $act['lpwdchange']

if (strtotime('-1 day') < strtotime($act['lpwdchange'])) {
    // Code Here
}

Upvotes: 12

Related Questions