how to sort by modification date?...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

how to sort by modification date?...

Post 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]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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>";
}

?>
Post Reply