0

I need to revalidate cache if its expired. My cache data looks like this

$cacheData['valid_until'] = "2017-11-23T12:00:00+00:00" //string

I wonder how to properly compare if current dateTime is smaller then $cacheData['valid_until'], while taking into consideration also timezone...

This is my current code

 private function checkCacheValidation($cacheData) {
    $now = (new DateTime());
    $cacheTime = (new DateTime($cacheData['valid_until']));

    if ($now < $cacheTime) {
        die('Cache is valid, no need to request new data');
        return true;
    } else {
        die('cache not valid, get new data');
        return false;
    }
}

Can somebody please check if i'm doing this right way? Do you suggest any other solution?

If you need any additional informations, please let me know and i will provide... Thank you

Valor_
  • 3,461
  • 9
  • 60
  • 109

1 Answers1

1

As you can see in the following topic, you can compare DateTime variables:

$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);

Giving the result of:

bool(false)
bool(true)
bool(false)

So seems like a working solution.

pgndck
  • 328
  • 1
  • 15