0
        <?php
        //basic directory
        $dir    = 'dir';
       //hardcoded hours and minutes for specific time 
        $hours = 11 ; 
        $minutes = 2 ;

        date_default_timezone_set('Asia/Kolkata');
        $today = date('F d, Y');

            if (is_dir($dir)) { if ($dh = opendir($dir)) {

                    while (($file = readdir($dh)) !== false) {                
                        clearstatcache();

                        if(is_file($dir."/".$file)) {  
//$filename will store the  last modified files which in folder                  
                                $filename = filemtime($dir."/".$file);
            //condition for some relevent time
                                if(date('F d, Y',$filename) == $today && date("H", $filename) >= $hours && date("i", $filename) >= $minutes)
                                {
    // here i want the sorting code. because it doesnt display the time wise file in output
                                echo $file;
                                echo " - ";                    
                                echo "Last modified: " . date("F d, Y H:i:s.", $filename);
                                echo "<br>";
                                }
                         }  
                        }            
                        echo "<br>";
                        closedir($dh);
                    }
                }

            ?>
        /*
        i have this output. but it is not perfect i want the last modified come first
        through sorting .

       test2.php - Last modified: May 26, 2016 11:30:10.
        test3.php - Last modified: May 26, 2016 11:32:07.
        test.txt - Last modified: May 26, 2016 13:13:11.
        */
4

1 回答 1

0

您已经拥有所有详细信息(Unix 时间戳和路径)。要将这些部分放在一起,您可以使用时间戳作为键和路径作为值来创建一个数组(请记住,每个唯一时间戳可以有多个文件),然后根据自己的喜好使用ksort进行排序:

bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

按键对数组进行排序,维护数据相关性的关键。这主要用于关联数组。

这意味着一个小的但积极的重新设计:当您从目录中检索文件时,您不能再显示文件,您需要存储它们以供进一步处理并在以后显示它们。

于 2016-05-26T12:14:02.720 回答