Search code examples
phpcurlcache-control

Convert Cache-Control age to days, minutes or seconds?


I need to develop a code that identifies the cache time of each file in a given URL like this tool

For that I'm using PHP with CURL:

$website = 'https://stackoverflow.com';

$ch = curl_init($website);
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$resp = curl_exec($ch);
var_dump($resp);

But in the response I don´t see the age of cache files and the cache control is target as private.

To work around this problem, I had to grab the link from each image file, javascript, css existing in the body of the page and run the same CURL code as above.

And finally the cache-control would be shown, some show a huge date (max-age=315360000) others show a small date (max-age=604800), now remains to know how can I convert and identify when cache-control is referring to days, minutes, years or seconds ?


Solution

  • one can convert from seconds to human-readable format alike this:

    sprintf('%02d:%02d:%02d', ($s/3600), ($s/60%60), $s%60);