checking time on a file...

So I don’t hit the servers constantly I was trying to put in a way to ONLY check every 4 hours…Here is what I have…

[php]
if (time()-filemtime($myfile) > 4 * 3600) {
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$myfile=fopen(‘channel.json’, “w”);
$api = file_get_contents(“https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=$maxVids&playlistId=$id&key=$key”);
fwrite($myfile, $api);
fclose($myfile);
}
[/php]

Problem is it’s by passing the time check and every time I reload the page it writes to the file…

suggestion?

Thanks!

Well in case anyone was interested here’s the answer to that question…

[php]
define(‘UPDATE_INTERVAL’, 60 * 60 * 4); // set update interval, eg 4 hours
define(‘DATAFILE’, ‘data.json’); // file to store the data in

$timeSinceUpdated = 0;

// if the datafile exists
if (file_exists(DATAFILE)) {
// get the time when the datfile was last updated/created
$lastModified = filemtime(DATAFILE);
// get the time passed since the file has been updated/created
$timeSinceUpdated = time() - $lastModified;
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service