Jump to content

[SOLVED] Sorting array by date


garry

Recommended Posts

So i have an array with a bunch of arrays inside of it. Of the second arrays, each has a "date" key with a date and time formatted as "24 Oct 2008 05:00:00 AM".

 

What I want to do is to sort this array by date and time, ascending. I've had a look at the array sorting functions on the php.net website and haven't come up with anything i think can do it.. do you guys know any way?

Link to comment
https://forums.phpfreaks.com/topic/140923-solved-sorting-array-by-date/
Share on other sites

hmm, horrible format :P. if you were using unix timestamps you could do it with 1 loop.

 

as it stands you may be able to make use of 1 of php's native date and time functions:

 

http://uk.php.net/manual/en/function.strtotime.php.

 

once they are timestamps, ou can sort from lowest to highest -voila.

foreach ($dataArray as $key => $row) {
    $orderByDate[$key]  = strtotime($row['date']);
}

array_multisort($orderByDate, SORT_ASC, $dataArray);

Modify as necessary to suit your nested arrays when setting the $orderByDate keys in the loop

FYI if your working with dates before "January 1 1970 00:00:00 UTC", use caution with unix timestamps, some computers (older operating systems, solaris) cannot use negative timestamps, since the timestamp of "January 1 1970 00:00:00 UTC" is 0, anythig before that would be -x.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.