How to sort an array of dates and times?

PHP: How to sort an array of dates and times?

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:

First, convert the dates into a timestamp

A timestamp is time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). The current timestamp (when this page was last cached, anyway) is: 1575882558 (time()).

Because there is no standard PHP array function for sorting date strings we will have to convert it ourselves. But we can make a closure, and pass that to usort(&$array, $compare_func) (which lets us define our own function to do the comparisons for the sorting)

The custom comparison function for usort takes 2 parameters. It returns either -1, 0, or 1 depending on how they compare (1 if $a is less than $b; -1 if $a is greater than $b; 0 if equal). We can use PHP's spaceship operator for this (<=>) .

Let's set up the data:

  1.     $dates = [
  3.     "10 September 2000",
  4.     "10 September 2020",
  5.     "9 September 2020",
  6.     "9 September 2000",
  7.     "5 April 1999",
  8.     "20 April 1998",
  9. ];

And now the comparison function:

  1. $compare_function = function($a,$b) {
  3.         $a_timestamp = strtotime($a); // convert a (string) date/time to a (int) timestamp
  4.         $b_timestamp = strtotime($b);
  6.         // new feature in php 7
  7.         return $a_timestamp <=> $b_timestamp;
  9.         /* old way to do it:
  11.                 if ($a_timestamp > $b_timestamp) {
  12.                     return -1;
  13.                 }
  14.                 elseif ($a_timestamp < $b_timestamp) {
  15.                     return 1;
  16.                 }
  17.                 else {
  18.                     return 0;
  19.                 }
  21.         */
  23.                 };

Now we have everything we need - let's sort that data!

  1.         usort($dates, $compare_function);
  2.         var_dump($dates);
array:6 [
  0 => "20 April 1998"
  1 => "5 April 1999"
  2 => "9 September 2000"
  3 => "10 September 2000"
  4 => "9 September 2020"
  5 => "10 September 2020"
webdevetc profile pic

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This ( is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.


Comments and discussion about How to sort an array of dates and times?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to change the order that migrations happen?

How to hide the server signature (hide Apache info) from HTTP headers with .htaccess

How to make multiple routes share the same URL structure or URL parameters

How to review blog posts (written by other users) before they get published on your WordPress blog?

301 Redirect a single URL in .htaccess to a new location

What are the predefined variables in PHP?

How to disable file hotlinking in Apache with .htaccess

How do Invokable Controllers work in Laravel?

How to fix the MySQL related "Specified key was too long error" error in Laravel

How to find the 2nd most common item in an array?