Forum Moderators: coopster

Message Too Old, No Replies

comparing dates and file modifications

         

surrealillusions

10:07 pm on Jan 7, 2010 (gmt 0)

10+ Year Member



Hi all,

I know a little about this, but not enough to get all the code i need.

Basically, I would like find all the last modification dates of all the files in a folder, and compare them to the current date. Those that have been modified in the last 7 days, I can apply a class to that file, as they're ordered in a list. Can add the <li> a special class, and obviously style it in CSS which is easy enough. Anyway, the point is the PHP side of things.

Anyway, I have a function to get the last modified date of all the files, taken from php.net:

So if its possible to work it with that, then that'd be great, but if theres an easier way to combine the 2 then that's be great. As I would like to keep the 'last file updated'.


$directory = "/dir/";
function getAllFiles($directory, $recursive = true) {
$result = array();
$handle = opendir($directory);
while ($datei = readdir($handle))
{
if (($datei != '.') && ($datei != '..'))
{
$file = $directory.$datei;
if (is_dir($file)) {
if ($recursive) {
$result = array_merge($result, getAllFiles($file.'/'));
}
} else {
$result[] = $file;
}
}
}
closedir($handle);
return $result;
}

function getHighestFileTimestamp($directory, $recursive = true) {
$allFiles = getAllFiles($directory, $recursive);
$highestKnown = 0;
foreach ($allFiles as $val) {
$currentValue = filemtime($val);
if ($currentValue > $highestKnown) $highestKnown = $currentValue;
}
return $highestKnown;
}

echo 'last file updated: ';
echo date('l jS F Y', getHighestFileTimestamp('../'));

thanks :)

coopster

3:23 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you asking how to compare the dates to the current date to discover those 7 days old? The PHP Date and Time Related Extensions [php.net] are certainly options.

I guess if you needed both options, all files in the directory and/or only those a certain number of days old (or any period for that matter), you could keep the directory listing retrieval in one function and the date comparison in another. That way when you are looping through the results you can format as desired and exit the loop control structure when desired.