1

I want to cache file for 5 minutes but filemtime with different day always return false, here the code

<?php
error_reporting(E_ALL);
date_default_timezone_set("Asia/Jakarta");
$cache_file = 'myfile';
$cachetime = time() - 5*60;

if(filemtime($cache_file) >  $cachetime ) {
  echo "Cache Expired";
}
else{
  echo "File Mod: ".filemtime($cache_file)." >>> ".date("F d Y H:i:s", filemtime($cache_file))."<br>";
  echo "Time Now: ".$cachetime." >>> ".date("F d Y H:i:s", $cachetime)."<br>";
}
?>

and output

File Mod: 1431696549 >>> May 15 2015 20:29:09
Time Now: 1431716474 >>> May 16 2015 02:01:14

thanks for your help.

1
  • 1
    Less than, not greater than. If the cache was modified 5 minutes ago, then it is going to be less than the time now... Commented May 15, 2015 at 19:13

1 Answer 1

1

Looks like your logic is off. You want to create a timestamp of the given modification time PLUS the TTL, and compare this to the current time.

$cachetime = filemtime($cache_file) + 5*60;

if($cachetime > time()) {
  echo "Cache Expired";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.