garry Posted January 15, 2009 Share Posted January 15, 2009 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? Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted January 15, 2009 Share Posted January 15, 2009 Take a look at array_multisort. Note that you'll need to convert "24 Oct 2008 05:00:00 AM" to a PHP timestamp value for your orderBy array Quote Link to comment Share on other sites More sharing options...
uniflare Posted January 15, 2009 Share Posted January 15, 2009 hmm, horrible format . 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. Quote Link to comment Share on other sites More sharing options...
garry Posted January 15, 2009 Author Share Posted January 15, 2009 Sorry, not my format, it's from an XML feed So changing it to a unix timestamp shouldn't be too hard. Once I've done this, which loop would do it? (I had a look at arary_multisort and couldn't see an argument for a key to sort by?) Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted January 15, 2009 Share Posted January 15, 2009 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 Quote Link to comment Share on other sites More sharing options...
garry Posted January 15, 2009 Author Share Posted January 15, 2009 Worked great, strtotime converted the dates to unix very easily. Thanks both of you Quote Link to comment Share on other sites More sharing options...
uniflare Posted January 15, 2009 Share Posted January 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
garry Posted January 15, 2009 Author Share Posted January 15, 2009 Thanks for the note uniflare, all my dates will be recent but that's good to know Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.