Comparing two dates in PHP


To compare two dates in PHP, the code is as follows. Here, we have used the equality operator to compare dates −

Example

 Live Demo

<?php
   $dateOne = "2019-10-30";
   $dateTwo = "2019-10-30";
   echo "Date1 = $dateOne";
   echo "
Date2 = $dateTwo";    if ($dateOne == $dateTwo)       echo "
Both the dates are equal!";    else       echo "Both the dates are different!"; ?>

Output

This will produce the following output−

Date1 = 2019-10-30
Date2 = 2019-10-30
Both the dates are equal!

Example

Let us now see another example−

 Live Demo

<?php
   $dateOne = "2019-11-08";
   $dateTwo = "2018-08-10";
   echo "Date1 = $dateOne";
   echo "
Date2 = $dateTwo";    if ($dateOne < $dateTwo)       echo "
DateTwo is the latest date!";    else       echo "
DateOne is the latest date!"; ?>

Output

This will produce the following output−

Date1 = 2019-11-08
Date2 = 2018-08-10
DateOne is the latest date!

Updated on: 02-Jan-2020

500 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements