Given two dates (date1 and date2) and the task is to compare the given dates. Comparing two dates in PHP is simple when both the dates are in the same format but the problem arises when both dates are in a different format.
Method 1: If the given dates are in the same format then use a simple comparison operator to compare the dates.
Example:
<?php // PHP program to compare dates // Declare two dates and // initialize it $date1 = "1998-11-24"; $date2 = "1997-03-26"; // Use comparison operator to // compare dates if ($date1 > $date2) echo "$date1 is latest than $date2"; else echo "$date1 is older than $date2"; ?> |
1998-11-24 is latest than 1997-03-26
Method 2: If both of the given dates are in different formats then use strtotime() function to convert the given dates into the corresponding timestamp format and lastly compare these numerical timestamps to get the desired result.
Example:
<?php // PHP program to compare dates // Declare two dates in different // format $date1 = "12-03-26"; $date2 = "2011-10-24"; // Use strtotime() function to convert // date into dateTimestamp $dateTimestamp1 = strtotime($date1); $dateTimestamp2 = strtotime($date2); // Compare the timestamp date if ($dateTimestamp1 > $dateTimestamp2) echo "$date1 is latest than $date2"; else echo "$date1 is older than $date2"; ?> |
12-03-26 is latest than 2011-10-24
Method 3: Using DateTime class to compare two dates.
Example:
<?php // PHP program to compare dates // Declare two dates in different // format and use DateTime() function // to convert date into DateTime $date1 = new DateTime("12-11-24"); $date2 = new DateTime("2011-03-26"); // Compare the dates if ($date1 > $date2) echo $date1->format("Y-m-d") . " is latest than " . $date2->format("Y-m-d"); else echo $date1->format("Y-m-d") . " is older than " . $date2->format("Y-m-d"); ?> |
2012-11-24 is latest than 2011-03-26
Recommended Posts:
- Return all dates between two dates in an array in PHP
- How to store all dates in an array present in between given two dates in JavaScript ?
- Program to find the number of days between two dates in PHP
- How to calculate the difference between two dates in PHP?
- PHP | Number of week days between two dates
- Comparing float value in PHP
- Compare two dates using JavaScript
- How to calculate minutes between two dates in JavaScript ?
- How to calculate the number of days between two dates in javascript?
- How to check if one date is between two dates in JavaScript ?
- Calculating total number of Hours, Days, Minutes and Seconds between two dates in Golang
- Sort an array of dates in PHP
- How to compare date part only without comparing time in JavaScript?
- Percent Change & Discounts - Comparing Quantities | Class 8 Maths
- Class 8 NCERT Solutions - Chapter 8 Comparing Quantities - Exercise 8.2
- Compare Dates in Java
- How to select Min/Max dates in an array using JavaScript ?
- Get the relative timestamp difference between dates in JavaScript
- How to get highcharts dates in the x-axis ?
- How to run two PHP versions for two projects on same apache server ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

