如何比较两个碳时间戳? [英] How to compare two Carbon Timestamps?

查看:32
本文介绍了如何比较两个碳时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个时间戳,我创建的edited_at 和created_at(Laravel 的)...在数据库中,两者都有类型时间戳和默认值 0000-00-00 00:00:00 ... 但是

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) 给出字符串.而 var_dump(created_at variable) 是对象/碳.这些时间戳有什么问题?

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

在使用 format('U') 转换为整数后,我必须比较两者.我只能在 Carbon Object 上调用这个方法.我该怎么做?

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?

推荐答案

首先,Eloquent 自动将其时间戳 (created_at, updated_at) 转换为碳对象.您可以只使用 updated_at 来获得这个不错的功能,或者在您的模型中的 $dates 属性中指定 edited_at:

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'];

<小时>

现在回到你的实际问题.Carbon 有一堆比较函数:


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

  • eq() 等于
  • ne() 不等于
  • gt() 大于
  • gte() 大于等于
  • lt() 小于
  • lte() 小于等于
  • eq() equals
  • ne() not equals
  • gt() greater than
  • gte() greater than or equals
  • lt() less than
  • lte() less than or equals

用法:

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

这篇关于如何比较两个碳时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆