简体   繁体   中英

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?

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

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

Details on the stat() function here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM