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

skoobi's avatar
Level 13

Laravel Date comparison

Hi. For some reason my head wants to explode every time I get to this, and for some reason I just can't figure it out out even though its pretty straight forwards.

Im using Carbon for the dates and what I am trying to do is enable and disable a button in blade depending if the customers expiry date is due (within 7 days) or not.

So my logic is that I need to check that the $expiry is after Carbon::today()!!!

So in code it is ::

@if($expiry >= \Carbon\Carbon::today()->subDays(7)) disable @endif

But this doesn't disable the button!

Any ideas ?

many thanks

0 likes
8 replies
lostdreamer_nl's avatar

"depending if the customers expiry date is due (within 7 days) or not."

So the expiry date should be before 7 days in the future ;)

@if($expiry <= \Carbon\Carbon::today()->addDays(7)) disable @endif

You were now checking if expiry was after 7 days ago.

skoobi's avatar
Level 13

Thank you but unfortunately that didn't work... It just keeps showing!!!

Heres the dates ::

$expiry = 2018-12-05; // Should enable the button as its pithing 7 days of today
$expiry = 2018-12-07; // Should disable as its 8 days away

// Its parsed by carbon so I can use methods on the variable i.e. $expiry->addDays(7)

I just can't figure out whats going on!!

manelgavalda's avatar

@SKOOBI - If both are carbon instances you can compare them using gt greather than or lt lesser than.

In you case i think you should use gte "grater than or equal":

@if($expiry->gte(\Carbon\Carbon::today()->addDays(7))) disable @endif 
skoobi's avatar
Level 13

Hmm... I tried that as well and that didn't work either... Im going to have to look at the code and see what the heck going on!!! Or leave it as a bug for later :)

manelgavalda's avatar

@SKOOBI - Um, if it's not working with lte, then I don't know what it can be. Maybe the dates are carbon instances, but they don't have the value you expect.

skoobi's avatar
Level 13

Just checked the values and they're correct. Ive been pulling my hair out on this thinking it was the query I was asking, but seems its something else. Ill see if I can dig a bit deeper and figure it out.

Cheers

lostdreamer_nl's avatar

@skoobi

    $expiry = '2018-12-05';
    dd( $expiry <= \Carbon\Carbon::today()->addDays(7) );   

This outputs true

So if, in your view, $expiry is a string '2018-12-05', it should be outputing that 'disable'

But I think you are outputting this in some input [type=submit] ?

In that case: You want to output "disabled" (you missed the D) if you want to disable the button ;)

skoobi's avatar
Level 13

Im an idiot!!!!

Theres a very good reason it wasn't working as expected...

Changed the disable to disabled :) ... I want to cry.

<input type="submit" value="GO TO MY SHOPPING BASKET" @if($expiry <= \Carbon\Carbon::today()->addDays(7)) disabled @endif >
1 like

Please or to participate in this conversation.