doc1355 Posted January 20, 2022 Share Posted January 20, 2022 I have the following array, I want to sort by Date, from latest to oldest. How can I do that $array = array( 0 => array( 'id' => 5952, 'points' => 15, 'result' => 'W', 'date' => 'Jan. 2, 22', ), 1 => array( 'id' => 5965, 'points' => 6, 'result' => 'L', 'date' => 'Jan. 1, 22', ), ); Quote Link to comment https://forums.phpfreaks.com/topic/314436-sort-by-date/ Share on other sites More sharing options...
mac_gyver Posted January 20, 2022 Share Posted January 20, 2022 that date format is not directly sortable. where is this data coming from/stored at? you should store dates in a YYYY-MM-DD format, which will allow easy sorting by the date, then output the date in the format that you want when you display it. if this data is coming from a database, sort it in the sql query. if this data must be sorted in php, you would use php's usort() function, with a call-back function to sort on the date field. if that incoming date format cannot be corrected at the source, i would first convert the format, all at once, to be YYYY-MM-DD, then sort the data, then format the date back to that format when you display it. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314436-sort-by-date/#findComment-1593474 Share on other sites More sharing options...
Solution Barand Posted January 20, 2022 Solution Share Posted January 20, 2022 Example $arr = [ [ 'A', 'Jan. 22, 22'], [ 'B', 'Dec. 25, 21'], [ 'C', 'Feb. 22, 22'], [ 'D', 'Jan. 2, 22'] ]; usort($arr, function($a, $b) { $da = DateTime::createFromFormat('M. j, y', $a[1]); $db = DateTime::createFromFormat('M. j, y', $b[1]); return $db <=> $da; }); echo '<pre>' . print_r($arr, 1) . '</pre>'; outputs Array ( [0] => Array ( [0] => C [1] => Feb. 22 22 ) [1] => Array ( [0] => A [1] => Jan. 22 22 ) [2] => Array ( [0] => D [1] => Jan. 2 22 ) [3] => Array ( [0] => B [1] => Dec. 25 21 ) ) 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/314436-sort-by-date/#findComment-1593482 Share on other sites More sharing options...
doc1355 Posted January 20, 2022 Author Share Posted January 20, 2022 6 hours ago, mac_gyver said: that date format is not directly sortable. where is this data coming from/stored at? you should store dates in a YYYY-MM-DD format, which will allow easy sorting by the date, then output the date in the format that you want when you display it. if this data is coming from a database, sort it in the sql query. if this data must be sorted in php, you would use php's usort() function, with a call-back function to sort on the date field. if that incoming date format cannot be corrected at the source, i would first convert the format, all at once, to be YYYY-MM-DD, then sort the data, then format the date back to that format when you display it. Thank you. I got it to work. Quote Link to comment https://forums.phpfreaks.com/topic/314436-sort-by-date/#findComment-1593483 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.