I know there are many examples on Stack Overflow of how to compare two dates in PHP but I can't get my code to work properly.
Currently I have two dates the cache date and the hit date. I am trying to set it up that if the cache date is more than 2 hours ago from the hit date it will then recache new info. I currently have dates stored in MySQL in this format: $lastcache = date("m.d.y G:i");
My current code that works only about half the time (mostly in the later day for some reason) is this:
$lastcache = $row['lastcache'];
$expiredate = date("m.d.y G:i", strtotime("-120 minutes"));
if( $expiredate > $lastcache){
cache($input);
}else{
//do cool stuff
}
How can I better compare these two dates to see if more than 2 hours has passed?
This code works perfectly for me, updated with your variables:
$interval = (strtotime(date("Y-m-d H:i:s")) - strtotime($lastcache) ) / 60;
This will return the time interval, in minutes. Add math to format to whatever format you want.
Assuming you kept the interval result in minutes, you would just do:
if($interval >= 120) {...}
Please consider using PHP's Date/Time object . Documentation can be found here