0

I'm trying to compare these 2 DateTimes. I'm storing one in a database that is stored whenever a new row is inserted. The other one is the current DateTime, but I'm trying to add two minutes to it and then compare it.

My current code:

$time = $cached_data["last_update"];
$date = new DateTime ('now');
$datef = $date->format('Y-m-d H:i:s');

// Check if the cached data has expired and if it has then update the data
if($datef > ($time)) {

    echo "updated";
}

Echoing both the $datef and the $time gives:

$datef: 2016-04-06 00:23:43

$time: 2016-04-05 18:21:03

and the script echoes out "updated".

I realize the time on my system and the time of my database are different, but how would I add the two minutes to the $datefand would this work?

Joel E
  • 103
  • 1
  • 7
  • 2
    `$date->add(new DateInterval('PT2M'));` - [PHP Docs](http://www.php.net/manual/en/datetime.add.php) – Mark Baker Apr 05 '16 at 22:30
  • Possible duplicate of [Adding minutes to date time in PHP](http://stackoverflow.com/questions/8169139/adding-minutes-to-date-time-in-php) – fusion3k Apr 05 '16 at 22:44
  • Okay, so let's say instead of a DATETIME in my database, I used timestamp and time()... How would I check it then? – Joel E Apr 05 '16 at 23:18

0 Answers0