How to compare two dates using PHP?

How to compare two dates using PHP


When working with dates in PHP, it is often necessary to compare them to see the order. If you want to check if a date is leading or trailing, or equal, PHP provides a number of functions and methods to efficiently accomplish this task. In this guide, we will explore various ways to compare two dates in PHP and understand the underlying assumptions to make an accurate comparison.

We often need to verify whether the current date is between two dates or not for subscription, trial, user expiry time, and so on. When a user registers, he will determine when he will expire, and we will always compare it with the current date. So, perhaps this post can assist you in determining whether a date exists between two dates in PHP.

You can use the basic code given below to help you.

$currentDate = date('Y-m-d');
$currentDate = date('Y-m-d', strtotime($currentDate));

$startDate = date('Y-m-d', strtotime("01-01-2023"));
$endDate = date('Y-m-d', strtotime("01-12-2023"));

if( ( $currentDate >= $startDate ) && ( $currentDate <= $endDate ) ) {
	echo "Current date is between two dates";
	echo "Current date is not between two dates";  

If the current date is 15-06-2023 then it will return bellow output:

Current date is between two dates


In conclusion, evaluating dates in PHP is an essential mission that allows builders to handle date-associated operations efficiently. Throughout this manual, we’ve got explored various techniques and capabilities to be had in PHP to compare two dates accurately.

By know-how the fundamentals of representing dates in PHP and utilizing comparison operators and capabilities like strtotime(), mktime(), and DateTime elegance, you could perform date comparisons with ease.

Also Read:

How useful was this blog?

Click on a star to rate it!

Average rating 2.9 / 5. Vote count: 153

No votes so far! Be the first to rate this blog.

Leave a comment

Your email address will not be published. Required fields are marked *