How to sort date array in php?

I am new to php, I have php date array

[0] => 11-01-2012
[1] => 01-01-2014
[2] => 01-01-2015
[3] => 09-02-2013
[4] => 01-01-2013

I want to sort it like :

[0] => 11-01-2012
[1] => 01-01-2013
[2] => 09-02-2013
[3] => 01-01-2014
[4] => 01-01-2015

I use asort but not working.


Because array items is string, you need to convert them to date and then comparing to sort. The usort() sort array using custom function that is a good sort function for this case.

$arr = array('11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013');    
function date_sort($a, $b) {
    return strtotime($a) - strtotime($b);
}
usort($arr, "date_sort");
print_r($arr);

Check result in demo

Sort an array of dates in PHP, The strtotime() function is a PHP function which changes a given date in any format into a timestamp which is a large integer in nature and then while sorting the array we can easily use the PHP | usort() function by defining a comparator function. The strtotime() function is a PHP function which changes a given date in any format into a timestamp which is a large integer in nature and then while sorting the array we can easily use the PHP | usort() function by defining a comparator function. The comparator function will accept two date arguments which will be converted to integer timestamp using strtotime() function and then compared to sort date based on integer timestamp value.


out of the box use time function to generate ts and sort

<?php
  $out = array();
  // $your_array is the example obove
  foreach($your_array as $time) {
  $out[strtotime($time)] = $time;
  }
  // now $out has ts-keys and you can handle it

  ...
 ?>

ksort

Sort an array of dates in PHP, php function compareDates($date1, $date2){ if (strtotime($date1) < strtotime($date2)) return 1; else if (strtotime($date1) > strtotime($date2)) return -1; else return 0; } $dateArr = array("2019-11-11", "2019-10-10","2019-11-11", "2019-09-08","2019-05-11", "2019-01-01"); usort($dateArr, "compareDates"); print_r($ 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.


Try this,

<?php 
$array = [ '11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013' ];
function sortFunction( $a, $b ) {
    return strtotime($a) - strtotime($b);
}
usort($array, "sortFunction");
var_dump( $array );
?>

Will sort the dates into the order you want.

How to sort an array of dates - PHP, Hey everyone, a bit stuck with trying to put an array of dates into chronological order that I have retrieved from the database, and then reformat� EDIT: Your data is organized in an array of arrays. To better distinguish those, let's call the inner arrays (data) records, so that your data really is an array of records. usort will pass two of these records to the given comparison function date_compare() at a a time.


Try below code:

<?php 

$array = array('11-01-2012','01-01-2011','09-02-2013','01-01-2014','01-01-2015');

function cmp($a, $b)
{
    $a = date('Y-m-d', strtotime($a));
    $b = date('Y-m-d', strtotime($b));

    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}
usort($array, "cmp");

foreach ($array as $key => $value) {
    echo "[$key]=> $value <br>";
}
?>

How to sort an array of dates and times? (SOLVED), In PHP it is very easy to sort an array of dates and times. All you need to do is convert it to a (integer) timestamp, and then do a normal sort. Here are more details� PHP - Sort Functions For Arrays. In this chapter, we will go through the following PHP array sort functions: sort() - sort arrays in ascending order; rsort() - sort arrays in descending order; asort() - sort associative arrays in ascending order, according to the value; ksort() - sort associative arrays in ascending order, according to the key


You definitely can sort array to timestamps with the given code below, but I would like to drag your attention to the format of time I have used to store.. There are different benefits of storing time in such format. Read more about this here https://stackoverflow.com/a/59912185/7997043

function compareByTimeStamp($a, $b ) {
    return strtotime($b) - strtotime($a);
}
$arr = array("2020-02-11 00:00:00", "2020-02-13 00:00:00", "2020-02-08 00:00:00"); 
usort($arr, "compareByTimeStamp"); 
echo json_encode($arr);

Sorting Arrays - Manual, PHP has several functions that deal with sorting arrays, and this document exists to help sort it all out. The main differences are: Some sort based on the array keys � Note: All of these sort functions act directly on the array variable itself, as opposed to returning a new sorted array If any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable).


sort - Manual, Note: If two members compare as equal, their relative order in the sorted array is undefined. Note: Like most PHP sorting functions, sort() uses an implementation of � Quicksort. The pivot is chosen in if you want to sort by date $column = 2 array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. Associative keys will be maintained, but numeric keys will be re-indexed.


sort array by date php Code Example, Get code examples like "sort array by date php" instantly right from your google search results with the Grepper Chrome Extension. var array = [{id: 1, date: Mar 12 2012 10:00:00 AM}, {id: 2, date: Mar 8 2012 08:00:00 AM}]; How can I sort this array by the date element in order from the date closest to the current date and time down? Keep in mind that the array may have many objects, but for the sake of simplicity I used 2. Would I use the sort function and a custom


Sort array by date, Hi all, i'm trying to sort an array by date but not getting any clue , can anyone help me with this? example array : [ "id": "27 sort(array&$array[, int$sort_flags= SORT_REGULAR]) : bool This function sorts an array. lowest to highest when this function has completed.