Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Crazylife's avatar

How to compare date with today date to know whether the date is greater or not?

I am using the method below to get the difference between day but it always show positive value even if the today date is greater than expiry date. I want to compare the date to know whether the product is expired or not.

var day = "{{(Carbon\Carbon::parse($product->expiry_date)->diffInDays(Carbon::now()))}}";
0 likes
5 replies
tisuchi's avatar

Try simply this-

$date = new Carbon;
if($date > $product->expirey_date)
{
    //its already expired
} else {
    //stil to go 
}
1 like
Crazylife's avatar

How if i need get to know whether it has 1 week more before the expiry date?

developre's avatar

Use subweek or subdays(days) function to get the date berfore your date.

$date->subWeek(); or $date->subDays(7);

Crazylife's avatar

I have stored expiry date to the database. I need to notify user when the product is going to expire which one week before.

Please or to participate in this conversation.