RegEdit
RegEdit

Reputation: 2162

Compare creation date/time of a file to current date/time?

This script takes an incoming search string ($query) and checks to see if a file by the same name already appears in the "files" directory. If it exists and is non-empty, the script prints the contents back to the page.

$searchCacheFile = dirname(__FILE__).'/files/'.$query.".txt";

if (file_exists($searchCacheFile) && is_readable($searchCacheFile))
{
    $retarr = file_get_contents($searchCacheFile);
    if($retarr !=="")
    {
        print_r($retarr);die;
    }
}

I want to add a check to determine the file creation date/time of the $searchCacheFile and compare it to the current date/time to see if more than 48 hours has passed since the file creation.

What method would you use to do the date/time comparison?

Upvotes: 1

Views: 6936

Answers (2)

Marc B
Marc B

Reputation: 360872

$stats = stat($searchfileCache);
if ($stats[9] > (time() - (86400 * 2)) {
   ... file is less than 2 days old ...
}

Details on the stat() function here.

Upvotes: 4

gargantuan
gargantuan

Reputation: 8944

Filetime should do the trick.

http://php.net/manual/en/function.filemtime.php

It returns a timestamp, so get the current timestamp, subtract the filtime timestamp and see if the difference is more that 60*60*48

Upvotes: 4

Related Questions