Avatar of Coronatus
Coronatus

asked on 

Sorting an Array containing Date Values

Greetings,

I got a directory on my web server containing folders which are named using the "DD.MM.YYYY" scheme. They contain server logs and get filled and created automatically by another script. Now I'm trying to create an index.php file to display this folder list sorted.

I'm using the code listed below. However, I get an alphabetical sorting rather than a date sorting in the resulting list. Which is of course a pain to search. Is there a better way to get this done? I know that PHP can be tricky when it comes to sorting an array containing timestamps.

Any help would be appreciated!

Thanks,
Coronatus :-)
<?
	if($dir == "")
	{
		$directory = opendir(".");
	}
	else
	{
		$directory = opendir($dir);
	}
	
	while($entry = readdir($directory))
	{
		$listing[] = $entry;
	}
	
	closedir($directory);
	$number = count($listing);
	sort($listing);
	
	for($index = 0; $index < $number; $index++)
	{
		if(substr("$listing[$index]", 0, 1) != ".")
		{
			if(filetype($listing[$index]) == "dir")
			{
				print("<img src=\"folder.gif\">&nbsp;<a href=\"index.php?dir=" . $listing[$index] . "\">" . $listing[$index] . "</a><br>\n");
			}
			else
			{
				if($dir != "")
				{
					print("<img src=\"entry.gif\">&nbsp;<a href=\"" . $dir . "/" . $listing[$index] . "\">" . $listing[$index] . "</a><br>\n");
				}
			}
		}
	}
?>

Open in new window

PHP

Avatar of undefined
Last Comment
Coronatus

8/22/2022 - Mon