Sort array by date in descending order by date in php

I have an array and I want to sort it by date. I cannot sort it correctly by date in descending order. Please, help.

Array ( [1] => Array ( [1] => 11/05/2013 [2] => Executive Planning Day ) [2] => Array ( [1] => 13/06/2013 [2] => Middle Leaders Planning Day ) [3] => Array ( [1] => 12/07/2013 [2] => New Staff Induction Day ) [4] => Array ( [1] => 13/04/2013 [2] => Staff Conference Day No. 1 ) [5] => Array ( [1] => 14/04/2013 [2] => Staff Conference Day No. 2 ) [6] => Array ( [1] => 15/02/2013 [2] => Staff Conference Day No. 3 ) [7] => Array ( [1] => 16/03/2013 [2] => Australia Day ) ) 
+6
source share
9 answers

try it

 function sortFunction( $a, $b ) { return strtotime($a[1]) - strtotime($b[1]); } usort($data, "sortFunction"); //Here You can use asort($data,"sortFunction") 

or you can try the detailed description (just his suggestion)

 function sortFunction($a,$b) if ($a[1] == $b[1]) return 0; return strtotime($a[1]) - strtotime($b[1]); } usort($data,"sortFunction"); 

Since strtotime does not obey the d / m / Y format , try this

 $orderByDate = $my2 = array(); foreach($data as $key=>$row) { $my2 = explode('/',$row[1]); $my_date2 = $my2[1].'/'.$my2[0].'/'.$my2[2]; $orderByDate[$key] = strtotime($my_date2); } array_multisort($orderByDate, SORT_DESC, $data); 
+20
source

Use usort() :

 function cmp($a, $b) { if ($a[1] == $b[1]) return 0; return (strtotime($a[1]) < strtotime($b[1])) ? 1 : -1; } usort($data, "cmp"); 
+2
source

Not quite happy with all the answers here, so I thought I wanted to point out that if you want to sort an associative array that stores keys, then you should use uasort , not usort . You can also parse a date from any format you like using the DateTime library, which also includes some predefined constants for some standard formats.

 uasort($array, function($a, $b){ $format = 'd/m/Y'; $ascending = false; $zone = new DateTimeZone('UTC'); $d1 = DateTime::createFromFormat($format, $a[1], $zone)->getTimestamp(); $d2 = DateTime::createFromFormat($format, $b[1], $zone)->getTimestamp(); return $ascending ? ($d1 - $d2) : ($d2 - $d1); }); 

demonstration

+2
source

I would build an array for ordering.

 $ordered = array(); foreach ($planning as $event) { $ordered[$event['date']] = $event; } ksort($ordered); 
+1
source

Use usort (Sort an array by values ​​using a custom comparison function).

 usort($array, function($a1, $a2) { $value1 = strtotime($a1['date']); $value2 = strtotime($a2['date']); return $value1 - $value2; }); 
+1
source

I spent the night working on how to do this for my own similar problem. Sort associative array by key date in this array.

Both usort and uasort require a sort function, which you must write and pass as the second parameter. The sort function is used by usort and uasort to compare each element in the array and save the result as $ yourArray

  function sortFunction( $a, $b ) { return strtotime($a[1]) - strtotime($b[1]); } uasort($yourArray, "sortFunction"); 
+1
source

@ Gautam3164's solution is almost perfect. You need to change the date format. I would say:

 function sortFunction( $a, $b ) { return strtotime(str_replace('/', '-',$a[1])) - strtotime(str_replace('/', '-',$b[1])); } usort($data, "sortFunction"); //Here You can use asort($data,"sortFunction") 

11/10/1987 → November 10, 1987 11-10-1987 → October 11, 1987

0
source
 $sortdate = array( '17/08/2015', '02/01/2017', '05/02/2014' ); function sortFunction($a, $b) { $datea = strtotime(str_replace('/', '-', $a)); $dateb = strtotime(str_replace('/', '-', $b)); if ($datea == $dateb) { return 0; } return ($datea < $dateb) ? -1 : 1; } usort($sortdate, "sortFunction"); echo "<pre>"; var_dump($sortdate); 
0
source

I jumped here to sort associative arrays and found this amazing function at http://php.net/manual/en/function.sort.php . This function is very dynamic, sorting in ascending and descending order with the specified key.

A simple function to sort an array by a specific key. Supports index association.

 <?php function array_sort($array, $on, $order=SORT_ASC) { $new_array = array(); $sortable_array = array(); if (count($array) > 0) { foreach ($array as $k => $v) { if (is_array($v)) { foreach ($v as $k2 => $v2) { if ($k2 == $on) { $sortable_array[$k] = $v2; } } } else { $sortable_array[$k] = $v; } } switch ($order) { case SORT_ASC: asort($sortable_array); break; case SORT_DESC: arsort($sortable_array); break; } foreach ($sortable_array as $k => $v) { $new_array[$k] = $array[$k]; } } return $new_array; } $people = array( 12345 => array( 'id' => 12345, 'first_name' => 'Joe', 'surname' => 'Bloggs', 'age' => 23, 'sex' => 'm' ), 12346 => array( 'id' => 12346, 'first_name' => 'Adam', 'surname' => 'Smith', 'age' => 18, 'sex' => 'm' ), 12347 => array( 'id' => 12347, 'first_name' => 'Amy', 'surname' => 'Jones', 'age' => 21, 'sex' => 'f' ) ); print_r(array_sort($people, 'age', SORT_DESC)); // Sort by oldest first print_r(array_sort($people, 'surname', SORT_ASC)); // Sort by surname 
0
source

Source: https://habr.com/ru/post/945766/


All Articles