How to compare two Carbon Timestamps?
Asked Answered
G

4

133

I have two timestamps, edited_at which I created and created_at (Laravel's)... In database, both have type timestamp and default value 0000-00-00 00:00:00... But

var_dump(edited_at variable) is giving string. While var_dump(created_at variable) is object/Carbon. What is wrong with these timestamps?

I have to compare both after converting into integer using format('U'). I can only call this method on Carbon Object. How can I do that?

Gelsemium answered 16/4, 2015 at 19:21 Comment(3)
What exactly do you want to compare? do you want to know which is older/newer?Headman
yes, I want to campare older/newerGelsemium
carbon.nesbot.com/docs/#api-comparisonNnw
H
275

First, Eloquent automatically converts it's timestamps (created_at, updated_at) into carbon objects. You could just use updated_at to get that nice feature, or specify edited_at in your model in the $dates property:

protected $dates = ['edited_at'];

Now back to your actual question. Carbon has a bunch of comparison functions:

  • eq() equals
  • ne() not equals
  • gt() greater than
  • gte() greater than or equals
  • lt() less than
  • lte() less than or equals

Usage:

if($model->edited_at->gt($model->created_at)){
    // edited at is newer than created at
}
Headman answered 16/4, 2015 at 19:34 Comment(7)
How can I compare the dates only and not the time?Preponderant
@Preponderant The easiest is probably to do $date1->toDateString() == $date2->toDateString()Headman
I am testing and it is possible to use the comparison operators to compare Carbon objects. Is it a new feature?Intermixture
When using this, remember to handle nullable timestamps. Laravel will just convert the SQL null into a PHP null instead of a Carbon object, and you'll get call to a member function lt() on null.Fetch
you should use $some_cabron_date->endOfDay() or $some_cabron_date->startOfDay() with that functions to the desired operationEyre
@Headman or $date->isSameAs(now())Brownstone
So, you can use the normal PHP operators to compare Carbon and DateTime dates? @FetchSidhu
H
55

Carbon has a bunch of comparison functions with mnemonic names:

  • equalTo()
  • notEqualTo()
  • greaterThan()
  • greaterThanOrEqualTo()
  • lessThan()
  • lessThanOrEqualTo()

Usage:

 if($model->edited_at->greaterThan($model->created_at)){
     // edited at is newer than created at
 }

Valid for nesbot/carbon 1.36.2

if you are not sure what Carbon version you are on, run this

$composer show "nesbot/carbon"

documentation: https://carbon.nesbot.com/docs/#api-comparison

Hedges answered 26/3, 2019 at 21:31 Comment(0)
E
4

First, convert the timestamp using the built-in eloquent functionality, as described in this answer.

Then you can just use Carbon's min() or max() function for comparison. For example:

$dt1 = Carbon::create(2012, 1, 1, 0, 0, 0); $dt2 = Carbon::create(2014, 1, 30, 0, 0, 0); echo $dt1->min($dt2);

This will echo the lesser of the two dates, which in this case is $dt1.

See http://carbon.nesbot.com/docs/

Eft answered 14/7, 2016 at 23:56 Comment(0)
A
4

This is how I am comparing 2 dates, now() and a date from the table

@if (\Carbon\Carbon::now()->lte($item->client->event_date_from))
    .....
    .....
@endif

Should work just right. I have used the comparison functions provided by Carbon.

Azurite answered 23/8, 2020 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.