Posted By

lafayette on 03/11/11


Tagged

php time sort


Versions (?)

PHP sort files by time


 / Published in: PHP
 

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

  1. function sortByChangeTime($file1, $file2)
  2. {
  3. return (filectime($file1) < filectime($file2));
  4. }
  5. $files = glob('*.*'); // use scandir if you want hidden files too
  6. usort($files, 'sortByChangeTime'); // sort by callback
  7. var_dump($files); // dump sorted file list
  8.  
  9. //ALTERNATIVE VERSION
  10.  
  11. if($h = opendir($dir)) {
  12. $files = array();
  13. while(($file = readdir($h) !== FALSE)
  14. $files[] = stat($file);
  15.  
  16. // do the sort
  17. usort($files, 'your_sorting_function');
  18.  
  19. // do something with the files
  20. foreach($files as $file) {
  21. }
  22. }

Report this snippet  

You need to login to post a comment.