Skip to content

Instantly share code, notes, and snippets.

@corazzi
Last active March 30, 2016 10:10
Show Gist options
  • Save corazzi/33d6ef28f2d1a97f7c12 to your computer and use it in GitHub Desktop.
Save corazzi/33d6ef28f2d1a97f7c12 to your computer and use it in GitHub Desktop.
Get the directory's modified time based on when the last file or directory within it was updated
<?php
/**
* Get the directory's modified time based on when the last file within it was updated
*
* @param string Which directory to look within
* @param string A regular expression to match files
*
* @return int The timestamp
*/
function dirmtime($path = '', $regex = null)
{
$directory = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($directory);
if ($regex) {
$iterator = new RegexIterator($iterator, $regex, RecursiveRegexIterator::GET_MATCH);
}
$mtimes = [];
foreach ($iterator as $file) {
if (is_array($file)) {
foreach ($file as $f) {
$mtimes[] = filemtime($f);
}
continue;
}
$mtimes[] = filemtime($file);
}
return empty($mtimes) ? null : max($mtimes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment