Sort an Array in PHP by Date

Sorting an array in PHP is not as difficult as many people make it out to be. This quick tutorial will walk you through the steps necessary to sort a date array.

Sorting a PHP Array by Date

The first step is ensuring that the dates are placed into the array in proper format. When you add the date to the array, the best and most compatible way to do do is to run the PHP date function on the date value. This function works no matter what format the date is in, including a MySQL datetime column or a PHP timestamp.

So to enter dates into your array you can do something like:

$dateArray[] = date('Y-m-d');

//If you are iterating through something like a database with date's you could do something like:

$dateArray[] = date('Y-m-d', $row['date']);

One you have your date array set

All you need to do is run a  sorting function on the array.

Most recent date first


Oldest date first


I hope this quick tutorial will help you in your website development and design endeavors!


Urvish 8:11 am - 14th July:


Thank you…
This working fine….

bosilkovska 5:54 am - 14th February:

That simple…

Leave a Reply

* Required
** Your Email is never shared

Fork me on GitHub