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.
        */
Thamilhan
  • 13,040
  • 5
  • 37
  • 59

1 Answers1

0

You already have all the details (Unix timestamp and path). To put the pieces together, you can create an array using timestamp as key and paths as values (remember you can have more than one file per unique timestamp) and then sort with ksort to your liking:

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

Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

This implies a minor but positive redesign: you can no longer display files as you retrieve them from the directory, you need to store them for further processing and display them all later.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360