/ Published in: PHP
This is a function that selects files from a directory and orders them by the last time they were changed, in ascending or descending order. The snippet also calculates how much time passed since the file’s content was changed.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function Sort_Directory_Files_By_Last_Modified($dir, $sort_type = 'descending', $date_format = "F d Y H:i:s.") { foreach($files as $file) { if($file != '.' && $file != '..') { $diff = $now - $last_modified; if($days) { $time_passed_array['days'] = $days; } $diff = $diff - ($days * 3600 * 24); if($hours) { $time_passed_array['hours'] = $hours; } $diff = $diff - (3600 * $hours); if($minutes) { $time_passed_array['minutes'] = $minutes; } $seconds = $diff - ($minutes * 60); $time_passed_array['seconds'] = $seconds; 'timestamp' => $last_modified, 'time_passed' => $time_passed_array); } } if($sort_type == 'descending') { } } //Example of usage: $dir = '/home/public_html/my_directory/'; $array = Sort_Directory_Files_By_Last_Modified($dir); // Info Array $info = $array[0]; // Sort Type $sort_type = $array[1]; echo '<h3>'.$dir.'</h3>'; echo 'Order by: Last Modified ('.$sort_type.')<br>'; foreach($info as $key => $detail) { echo '<h4>'.$detail['file'].'</h4>'; echo 'Last Modified: '.$detail['date'].'<br>'; $time_passed = ''; foreach($detail['time_passed'] as $type => $value) { $time_passed .= $value." ".$type.", "; } echo $time_passed."nn"; }
URL: http://www.bitrepository.com/web-programming/php/sort-files-by-filemtime.html