问题描述
我有两个时间戳,我创建的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()
equalsne()
not equalsgt()
greater thangte()
greater than or equalslt()
less thanlte()
less than or equals
用法:
if($model->edited_at->gt($model->created_at)){
// edited at is newer than created at
}
这篇关于如何比较两个碳时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!