Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function. This function assigns new integral keys starting from zero to array elements.
boolean usort( $array, "function_name")
Parameters: This function accepts two parameters as shown in the above syntax and are described below:
- $array: This parameter specifies the array which u want to sort.
- function_name: This parameter specifies the name of a user-defined function which compares the values and sort the array specified by the parameter $array. This function returns an integer value based on the following conditions. If two argument are equal then it returns 0, If first argument is greater than second, it returns 1 and if first argument is smaller than second, it returns -1.
Return Value: This function returns Boolean type value. It returns TRUE in case of success and FALSE in case of failure.
We use strtotime to convert given time string to a timestamp object. Once we have timestamps, we subtract them to decide greater.
Array (  => Array ( [gfg] => GFG_2 [datetime] => 2019-02-13 11:29:45 )  => Array ( [gfg] => GFG_3 [datetime] => 2019-02-15 11:29:45 )  => Array ( [gfg] => GFG_1 [datetime] => 2019-02-22 11:29:45 ) )
- How to search by key=>value in a multidimensional array in PHP ?
- PHP multidimensional array search by value
- How to sort an Array in C# | Array.Sort() Method Set – 2
- Date after adding given number of days to the given date
- Multidimensional arrays in PHP
- Multidimensional Arrays in Java
- Sort an array of dates in PHP
- How to sort an array of dates in C/C++?
- Sort array of objects by object fields in PHP
- PHP | Sort array of strings in natural and standard orders
- How to get the first element of an array in PHP?
- PHP | Second most frequent element in an array
- How to delete an array element based on key in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.