Sort Array by date
| From: | Matt Palermo | Date: | Thu, 04 Dec 2003 22:15:31 +0000 |
| Subject: | Sort Array by date | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-171902@lists.php.net to get a copy of this message | ||
I have a bunch of dates in the form: MM-DD-YYYY in an array and I want to
sort them and display them in descending order. I have tried the usort()
function below and it's not working. Can anyone help me out here?
$menu_item = array();
$menu_item[] = "12-04-2003";
$menu_item[] = "11-19-2003";
$menu_item[] = "01-04-2004";
$menu_item[] = "12-24-2003";
$menu_item[] = "08-13-1982";
// sorting function
function date_file_sort($a, $b)
{
$a = strtotime($a);
$b = strtotime($b);
return strcmp($a, $b);
}
usort($menu_item, 'date_file_sort');
// output results
for($x = 0; $x < count($menu_item); $x++)
{
echo $menu_item[$x]."<br>";
}
Here are the the results I get:
01-04-2004
08-13-1982
12-04-2003
11-19-2003
12-24-2003
Anyone know why these results seem totally random? Please help.
Thanks,
Matt