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

Anotheruser's avatar

Carbon date - Determine is a date is before a specified date

Hi.

This is a simple problem but I cant find an obvious answer in the docs.

All I need to do if check if date_a is before date_b, both are carbon dates.

So if i have date_a is 2nd January 2015 and date_b is 1st January 2015 this would return false.

I know about creating carbon dates, i just need a pointer to how to compare the two carbon dates to check if date_a is before or after date_b.

Thanks.

0 likes
12 replies
Anotheruser's avatar

@ohffs thanks. Is it possible to do this within an eloquent query? so an equivalent of doing

$variations = Variation::where('created_at', '<' , $valuation->updated_at)->get();

to check if the created_at date/time is before the $valuation->updated_at date/time ?

Thanks

ssomenzi's avatar

If you don't want to chain methods and prefer to use <= operator do your test on the date property

$date_a->date <= $date_b->date  // false

this can work also in your query as long as $valuation->updated_at is a carbon istance

$variations = Variation::where('created_at', '<' , $valuation->updated_at->date)->get();
ohffs's avatar

@barrygee I think you can literally do what you've typed - give it a try :-) I don't have a laravel install on the machine I'm just now to test. The only gotcha might be that created_at and updated_at are automagically cast to and from carbon instances, and I can't remember if you still have to have a $dates = [ $extra_date, $birthday ]; array in your model to cast others if you needed it.

Anotheruser's avatar

@ohffs i did try :) but unfortunately it didn't work :( i basically need to query the database and only return Variations with a 'created_at' date prior to a specified date. Sounds simple but i havent found a way to do it yet.

ohffs's avatar

@barrygee I just noticed you have a ->date at the end - try it just as $valuation->updated_at (I'm assuming thats a timestamp from the migration).

Anotheruser's avatar

@ssomenzi these are the created_at updated_at dates automatically create by laravel. I tried your suggestion but it didnt work. Thanks though.

skliche's avatar

@barrygee It should work. E.g. I have a users table and fetch one of the records, let's say the record of Jane. Then something like ``\App\User::where( 'created_at', '<', $userJane->updated_at )->get();``` works just fine. I've just tried it with tinker. I'm not saying that the query makes sense, but it returns what it is supposed to return. What exactly are you trying to achieve?
What makes you say it doesn't work. Do you get any kind of error message or just not the records you think you should get?

ssomenzi's avatar

@barrygee my mistake: should be ->toDateTimeString() and not ->date

so your query:

$variations = Variation::where('created_at', '<' , $valuation->updated_at->toDateTimeString())->get();

but as @skliche said it should also work as

$variations = Variation::where('created_at', '<' , $valuation->updated_at)->get();

as the where method in the query builder should cast it to a string before using it in building the query.

Anotheruser's avatar

Problem solved. None issue. Using

$variations = Variation::where('created_at', '<' , $valuation->updated_at)->get();

Was working. I was looking at the wrong variation!!!!! Sleep deprived.

Thanks all for your help.

Please or to participate in this conversation.