php opendir sort by date code example
Example 1: php sort file names by date
<?php
$dirpath = getcwd();
$dirpath .= "\*.jpg";
$files = array();
$files = glob($dirpath);
usort($files, function($x, $y) {
return filemtime($x) < filemtime($y);
});
foreach($files as $item){
echo basename($item) . " => Last Modified On " . @date('F d, Y, H:i:s', filemtime($item)) . "<br/>";
}
?>
Example 2: php list directory files by date
function listdir_by_date($path){
$ar = [];
chdir($path);
array_multisort(array_map('filemtime', ($files = glob("*.*"))), SORT_DESC, $files);
foreach($files as $filename)
{
$ar[] = $filename;
}
return $ar;
}