Reputation: 2018
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
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
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