/ Published in: PHP
URL: http://www.bitrepository.com/web-programming/php/sort-files-by-filemtime.html
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
function Sort_Directory_Files_By_Last_Modified($dir, $sort_type = 'descending', $date_format = "F d Y H:i:s.") { $files = scandir($dir); 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]; foreach($info as $key => $detail) { $time_passed = ''; foreach($detail['time_passed'] as $type => $value) { $time_passed .= $value." ".$type.", "; } }
You need to login to post a comment.
