Compare two dates in PHP
This article demonstrates how to compare two dates in PHP.
1. Using strtotime() function
A simple solution is to convert both dates into Unix timestamps and compare the timestamps to determine the order of dates. You can use the strtotime() function to parse a string containing an English date format into a Unix timestamp, which then can be compared using comparison operators.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $first = strtotime("2017-12-25"); $second = strtotime("2018-10-12"); if ($first < $second) { echo "First date is less than the second date"; } else if ($first > $second) { echo "First date is more than the second date"; } else { echo "First date is same as the second date"; } ?> |
If your dates are always in the English date format ('YYYY-MM-DD'), you can perform simple string comparisons to determine if the first date is less than, greater than, or equal to the second date.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $first = "2017-12-25"; $second = "2018-10-12"; if ($first < $second) { echo "First date is less than the second date"; } else if ($first > $second) { echo "First date is more than the second date"; } else { echo "First date is same as the second date"; } ?> |
2. Using DateTime class
Alternatively, you could use the DateTime class to perform a comparison between two dates. The advantage of using this method is that it can parse any date and time string in the specified format. The following example shows how you can compare two DateTime objects using comparison operators:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $first = DateTime::createFromFormat("d-m-Y", "25-12-2017"); $second = DateTime::createFromFormat("d-m-Y", "12-10-2018"); if ($first < $second) { echo "First date is less than the second date"; } else if ($first > $second) { echo "First date is more than the second date"; } else { echo "First date is same as the second date"; } ?> |
To find the datetime difference between two dates, you can use the DateTime::diff() function. Since it returns the DateInterval object, use the DateInterval::format() function with the formatting option '%R%a' to get the number of days.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $first = DateTime::createFromFormat("d-m-Y", "25-12-2017"); $second = DateTime::createFromFormat("d-m-Y", "12-10-2018"); $interval = $first->diff($second); echo "Difference: " . $interval->format('%R%a days'), PHP_EOL; $sign = $interval->format('%R'); $days = $interval->format('%a'); if ($days === "0") { echo "First date is same as the second date"; } else if ($sign === "+") { echo "First date is less than the second date"; } else if ($sign === "-") { echo "First date is more than the second date"; } ?> |
That’s all there is to comparing two dates in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)