Page 1 of 1

how to sort by modification date?...

Posted: Mon Oct 11, 2004 11:04 pm
by sebnewyork
feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi all
I'm trying to have the contents of a folder listed and sorted by last date they were modified.

I've got the list but can't figure out how to sort my items by date.
here's part of my code ($page is each item in the pages folder):

Code: Select all

foreach($pages as $page){
			$lm = date ('Y, m, g:i a', filemtime ("pages/$page"));
			$time_array = explode (',', $lm);
			ksort ($time_array);
			$time_array = implode (',', $time_array);
			print "<p>$time_array ($page)</p>"; 
		}
I've tried all sorts of sort (rsort, arsort, krsort, etc.) but the list is never sorted by date. It seems to be sorting the elements of the date within each date, but not the dates themselves...

Thanks for your help, and please, note: I am new to php.


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Oct 11, 2004 11:53 pm
by timvw
why are converting to date ???

Code: Select all

<?php
$dirpath = getcwd() . "/";
$dir = opendir($dirpath);
$files = array();
while ($file = readdir($dir)) {
   $localpath = $dirpath.$file;
  
   if (is_file($localpath)) {
       $key = filemtime($localpath).md5($file);
       $files[$key] = $file;
   }
}
ksort($files);
foreach ($files as $file) {
   echo "$file<br>";
}

?>