How to compare two dates in PHP?

Spread the love

Sometimes, we want to compare two dates in PHP.

In this article, we’ll look at how to compare two dates in PHP.

How to compare two dates in PHP?

To compare two dates in PHP, we can compare them with comparison operators.

For instance, we write

$today = date("Y-m-d");
$expire = $row->expireDate;

$today_time = strtotime($today);
$expire_time = strtotime($expire);

if ($expire_time < $today_time) {
    //...
}

to call strtotime with the $today and $expire date strings to convert them to dates.

Then we check if $expire_time is less than $today_time with

$expire_time < $today_time

Conclusion

To compare two dates in PHP, we can compare them with comparison operators.

Leave a Reply

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