laravellaravel-5php-carbon

How to compare two Carbon Timestamps?


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?


Solution

  • First, Eloquent automatically converts its 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:

    Usage:

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