How to compare two dates using PHP

Written by Ian Carnaghan · 15 sec read >
  • First we need to convert the two dates to UNIX Timestamps using the PHP function strtotime
  • Then we can compare the two dates in anyway we wish, in the example below I’m checking that the first date is older than the second date 
    $date1 = '1999-10-11';
    $date2 = '2010-10-11';
    $firstDate = strtotime($date1);
    $secondDate = strtotime($date2);
    if($firstDate < $secondDate){
         echo 'First date is older';
         echo 'Second date is older';

Last Updated On March 22, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

Leave a Reply

Notify of