Posted By

ryanve on 01/28/13


Tagged

time timestamp


Versions (?)

mtime: modified time


 / Published in: PHP
 

Get modified time of a directory or file. For directories: it gets the modified time of the most recently modified file.

  1. /**
  2.  * mtime() Get the modified time of a directory or file. For directories,
  3.  * it gets the modified time of the most recently modified file.
  4.  *
  5.  * @param string $path Full path to directory or file.
  6.  * @param string $format Date string for use with date()
  7.  * @return number|string|null
  8.  */
  9. function mtime ( $path, $format = null ) {
  10. $time = null;
  11. if ( \is_string($path) && \is_readable($path) ) {
  12. if ( \is_dir($path) ) {
  13. $path = \rtrim($path, '/');
  14. foreach ( \scandir($path) as $file ) {
  15. if ( '.' !== \substr($file, 0, 1) ) {
  16. $temp = mtime($path . '/' . $file);
  17. $temp > $time and $time = $temp;
  18. }
  19. }
  20. } elseif ( \file_exists($path) ) {
  21. $time = \filemtime($path);
  22. }
  23. }
  24. return $time && $format ? \date($format, $time) : $time;
  25. }

Report this snippet  

You need to login to post a comment.