0

I am trying to order an array by its date. I have tried to do this using usort but it will not change the order of the array in any way?

I am trying to show oldest near the start, and newest near the bottom.

usort($history, function($a, $b) {
    return strtotime($a['date']) < strtotime($b['date']) ? 1 : -1;
});

Array structure:

array:3 [
    0 => array:3 [
        "date" => "13/02/2020",
        "event" => "validate",
        "note" => "",
    ]

    1 => array:3 [
        "date" => "02/02/2020",
        "event" => "created",
        "note" => "",
    ]

    2 => array:3 [
        "date" => "03/04/2020",
        "event" => "approved",
        "note" => "",
    ]
]
AAA
  • 361
  • 1
  • 5
  • 19
  • Does this answer your question? [Sort multi-dimensional array by specific key](https://stackoverflow.com/questions/4022289/sort-multi-dimensional-array-by-specific-key) – sandip bharadva Mar 25 '21 at 10:50
  • `DD/MM/YYYY` is not a [valid date format](https://www.php.net/manual/en/datetime.formats.date.php) in PHP. Use `MM/DD/YYYY` or `YYYY-MM-DD` – Liftoff Mar 25 '21 at 10:50
  • Have you tried checking what `strtotime` returns for your dates? You'd be better off using `DateTime`. – El_Vanja Mar 25 '21 at 10:51
  • BTW If this data came from a database it would be a lot easier to order it in the SQL query – ADyson Mar 25 '21 at 10:58

0 Answers0