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
rsort($dateArray);
Oldest date first
sort($dateArray);
I hope this quick tutorial will help you in your website development and design endeavors!



1 Comment
Hi
Thank you…
This working fine….