PHP order array by date

Ran into a really interesting way to sort an array by a date field. I thought I would share this as it wasn’t super common to find. I found the answer in this Stackoverflow post. The third one down to me was the best answer. The reason why I felt that was it used an anonymous function instead of having to create a new function. It’s very compact and easy to understand that way. With this knowledge you are able to sort by more than just date columns to.


usort($array, function($a1, $a2) {

$v1 = strtotime($a1['date']);

$v2 = strtotime($a2['date']);

return $v1 - $v2;;

// $v2 - $v1 to reverse direction });

Leave a Reply

Your email address will not be published. Required fields are marked *