Re: Sort Array by date
| From: | Luke | Date: | Thu, 04 Dec 2003 22:33:32 +0000 |
| Subject: | Re: Sort Array by date | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-171911@lists.php.net to get a copy of this message | ||
an easy way, but might not work in your case is to use sort()
if instead of storing the dates like 12-04-2003, could you prehaps store
them as
2003-04-03
year-month-day
and then you could use
$datearray = sort($datearray, SORT_STRING);
would this work for you?
--
Luke
"Matt Palermo" <webmaster@sweetphp.com> wrote in message
news:20031204221535.55971.qmail@pb1.pair.com...
> 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