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?
First, Eloquent automatically converts it's timestamps (
created_at
,updated_at
) into carbon objects. You could just useupdated_at
to get that nice feature, or specifyedited_at
in your model in the$dates
property:Now back to your actual question. Carbon has a bunch of comparison functions:
eq()
equalsne()
not equalsgt()
greater thangte()
greater than or equalslt()
less thanlte()
less than or equalsUsage:
First, convert the timestamp using the built-in eloquent functionality, as described in this answer.
Then you can just use Carbon's
min()
ormax()
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/