bigtimslim Posted April 22, 2008 Share Posted April 22, 2008 I have an array with dates in it in the form: dd mmm yyyy What is the best way to sort this to most recent first? Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/ Share on other sites More sharing options...
velocite Posted April 22, 2008 Share Posted April 22, 2008 Try this mysql_query = ("SELECT * FROM table ORDER BY dates DESC"); Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523736 Share on other sites More sharing options...
Barand Posted April 22, 2008 Share Posted April 22, 2008 I have an array with dates in it in the form: dd mmm yyyy What is the best way to sort this to most recent first? Step 1 - change your dates to a sortable format Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523737 Share on other sites More sharing options...
bigtimslim Posted April 22, 2008 Author Share Posted April 22, 2008 velocite, there is no database involved. Barand, I should'nt have any problem changing the format... what format would you suggest Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523740 Share on other sites More sharing options...
Barand Posted April 22, 2008 Share Posted April 22, 2008 ISO date format YYYY-MM-DD Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523742 Share on other sites More sharing options...
bigtimslim Posted April 22, 2008 Author Share Posted April 22, 2008 ISO date format YYYY-MM-DD Ok. So what's Step 2? Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523745 Share on other sites More sharing options...
Barand Posted April 22, 2008 Share Posted April 22, 2008 What does the array look like? Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523746 Share on other sites More sharing options...
bigtimslim Posted April 22, 2008 Author Share Posted April 22, 2008 each entry has an index, and each index has a date, title, desc, link Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523749 Share on other sites More sharing options...
retro Posted April 22, 2008 Share Posted April 22, 2008 You should indeed use the standard YYYY-MM-DD. However, you could stick with British date formatting, and convert to ISO format. Say you had an array that was just dates, e.g. $DateArray = array(); $DateArray[] = "04-10-2007"; $DateArray[] = "15-06-2008"; $DateArray[] = "07-02-2008"; $DateArray[] = "22-01-2007"; $DateArray[] = "14-07-1987"; You could create a function to convert the date.... function SortDate($a, $b) { list($x, $y, $z) = explode("-", $a); $a = "$z-$y-$x"; list($x, $y, $z) = explode("-", $b); $b = "$z-$y-$x"; return -strcmp($a, $b); } and sort the array using the function... usort($DateArray, 'SortDate'); *EDIT* Apologies if you just read that, I failed to take into account that you wanted the most recent date first. I have changed the code to -strcmp in order to do this (without the - it would have listed oldest date first). Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523751 Share on other sites More sharing options...
Barand Posted April 22, 2008 Share Posted April 22, 2008 if date is in the first position you can just use rsort() <?php $array = array ( 0 => array ('2008-03-25', 'title 3', 'description 3', 'link 3'), 1 => array ('2008-02-03', 'title 4', 'description 4', 'link 4'), 2 => array ('2008-01-22', 'title 5', 'description 5', 'link 5'), 3 => array ('2008-04-23', 'title 1', 'description 1', 'link 1'), 4 => array ('2008-04-01', 'title 2', 'description 2', 'link 2'), ); rsort($array); echo '<pre>', print_r($array, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523752 Share on other sites More sharing options...
bigtimslim Posted April 22, 2008 Author Share Posted April 22, 2008 Awesome. Thanks so much. Link to comment https://forums.phpfreaks.com/topic/102283-solved-sort-array-by-date/#findComment-523758 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.