How to compare two Carbon Timestamps?

后端 未结 4 659
你的背包
你的背包 2020-11-29 17:52

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

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 18:29

    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
    }
    

提交回复
热议问题