How to compare two Carbon Timestamps?

后端 未结 4 631
你的背包
你的背包 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:36

    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

提交回复
热议问题