List contents of a directory by date in PHP

Sometimes, you need to list all contents of a directory, for example, to display all images in a specified directory. Unfortunately, PHP native functions to handle files and directories have a very limited capabilities. For example readdir, opendir and much more only takes 1 parameter. You might wonder how to list contents of a directory by date.

Here is one solution for that problem:

function listdir_by_date($path){
    $dir = opendir($path);
    $list = array();
    while($file = readdir($dir)){
        if ($file != '.' and $file != '..'){
            //To make sure you will not overwrite an array key, add filename at the end of datetime
            $ctime = filectime($data_path . $file) . ',' . $file;
            $list[$ctime] = $file;
    return $list;

Pretty simple, huh?