Sorting files by date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lion cave
    New Member
    • Apr 2009
    • 23

    #1

    Sorting files by date

    Hello,

    I would like to ask on how to sort files by date modified and displaying the top 20 latest date.

    Heres, my sample function that would return the files to be displayed


    Code:
    function get_dir_file($dir_path)
    {
    $file_arr = array();
    
        if ($handle = opendir($dir_path)) {
           while (false !== ($file = readdir($handle))) 
    	{
    	if ($file != "." && $file != ".." ) 
    	{
    	    $file_arr[] = $file;
    	}
    	}
         closedir($handle);
    }
    
    return file_arr;
    }
    This function should return an array that are sorted already.

    I would like to ask how to sort this files by date (return and order by: only top 20 latest date).

    Any suggestions is appreaciated.. Thnks.:)
    Last edited by Markus; Apr 17 '09, 10:27 AM. Reason: Fixed [code] tags.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    looks like a job for filemtime().

    Comment

    • lion cave
      New Member
      • Apr 2009
      • 23

      #3
      how to sort date

      Thanks for that suggestion..

      I would like to ask on how to sort dates. from the latest date.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I'd try something like: making an associative array with the filenames associated to the timestamps. then you can use arsort() (or related) to sort the array, cut off after 20 elements and extract (if needed) the filenames.

        Comment

        Working...