Return to Snippet

Revision: 42872
at March 11, 2011 21:07 by lafayette


Updated Code
function sortByChangeTime($file1, $file2)
{
    return (filectime($file1) < filectime($file2)); 
}
$files = glob('*.*');              // use scandir if you want hidden files too
usort($files, 'sortByChangeTime'); // sort by callback
var_dump($files);                  // dump sorted file list

//ALTERNATIVE VERSION

if($h = opendir($dir)) {
  $files = array();
  while(($file = readdir($h) !== FALSE)
    $files[] = stat($file);

  // do the sort
  usort($files, 'your_sorting_function');

  // do something with the files
  foreach($files as $file) {
    echo htmlspecialchars($file);
  }
}

Revision: 42871
at March 11, 2011 21:06 by lafayette


Initial Code
function sortByChangeTime($file1, $file2)
{
    return (filectime($file1) < filectime($file2)); 
}
$files = glob('*.*');              // use scandir if you want hidden files too
usort($files, 'sortByChangeTime'); // sort by callback
var_dump($files);                  // dump sorted file list

Initial URL
http://stackoverflow.com/questions/2325650/sorting-files-by-creation-modification-date-in-php

Initial Description

                                

Initial Title
PHP  sort files by time

Initial Tags
php, sort

Initial Language
PHP