How to sort a date array in PHP
Posted
by Click Upvote
on Stack Overflow
See other posts from Stack Overflow
or by Click Upvote
Published on 2009-02-28T11:00:48Z
Indexed on
2010/06/08
10:42 UTC
Read the original article
Hit count: 316
I have an array in this format:
Array
(
[0] => Array
(
[28th February, 2009] => 'bla'
)
[1] => Array
(
[19th March, 2009] => 'bla'
)
[2] => Array
(
[5th April, 2009] => 'bla'
)
[3] => Array
(
[19th April, 2009] => 'bla'
)
[4] => Array
(
[2nd May, 2009] => 'bla'
)
)
I want to sort them out in the ascending order of the dates (based on the month, day, and year). What's the best way to do that?
Originally the emails are being fetched in the MySQL date format, so its possible for me to get the array in this state:
Array
[
['2008-02-28']='some text',
['2008-03-06']='some text'
]
Perhaps when its in this format, I can loop through them, remove all the '-'
(hyphen) marks so they are left as integars, sort them using array_sort()
and loop through them yet again to sort them? Would prefer if there was another way as I'd be doing 3 loops with this per user.
Thanks.
Edit: I could also do this:
$array[$index]=array('human'=>'28 Feb, 2009',
'db'=>'20080228',
'description'=>'Some text here');
But using this, would there be any way to sort the array based on the 'db' element alone?
Edit 2: Updated initial var_dump
© Stack Overflow or respective owner