laravel 如何比较两个碳时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29684111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two Carbon Timestamps?
提问by Hassan Saqib
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
我有两个时间戳,我创建的edited_at和created_at(Laravel的)......在数据库中,两者都有类型时间戳和默认值0000-00-00 00:00:00......但是
var_dump(edited_at variable)
is giving string. While var_dump(created_at variable)
is object/Carbon. What is wrong with these timestamps?
var_dump(edited_at variable)
正在给字符串。虽然var_dump(created_at variable)
是物体/碳。这些时间戳有什么问题?
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?
在使用 format('U') 转换为整数后,我必须比较两者。我只能在 Carbon Object 上调用这个方法。我怎样才能做到这一点?
回答by lukasgeiter
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:
首先,Eloquent 会自动将它的时间戳 ( created_at
, updated_at
) 转换为 carbon 对象。您可以使用updated_at
来获得那个不错的功能,或者edited_at
在$dates
属性中的模型中指定:
protected $dates = ['edited_at'];
Now back to your actual question. Carbon has a bunch of comparison functions:
现在回到你的实际问题。Carbon 有一堆比较函数:
eq()
equalsne()
not equalsgt()
greater thangte()
greater than or equalslt()
less thanlte()
less than or equals
eq()
等于ne()
不等于gt()
比...更棒gte()
大于或等于lt()
少于lte()
小于或等于
Usage:
用法:
if($model->edited_at->gt($model->created_at)){
// edited at is newer than created at
}
回答by Yevgeniy Afanasyev
Carbon has a bunch of comparison functions with mnemonic names:
Carbon 有一堆带有助记符名称的比较函数:
- 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
适用于 nesbot/carbon 1.36.2
if you are not sure what Carbon version you are on, run this
如果您不确定自己使用的是哪个 Carbon 版本,请运行此命令
$composer show "nesbot/carbon"
documentation: https://carbon.nesbot.com/docs/#api-comparison
回答by ginna
First, convert the timestamp using the built-in eloquent functionality, as described in this answer.
首先,使用内置的 eloquent 功能转换时间戳,如本答案所述。
Then you can just use Carbon's min()
or max()
function for comparison. For example:
然后你可以只使用 Carbon 的min()
或max()
函数进行比较。例如:
$dt1 = Carbon::create(2012, 1, 1, 0, 0, 0);
$dt2 = Carbon::create(2014, 1, 30, 0, 0, 0);
echo $dt1->min($dt2);
$dt1 = Carbon::create(2012, 1, 1, 0, 0, 0);
$dt2 = Carbon::create(2014, 1, 30, 0, 0, 0);
echo $dt1->min($dt2);
This will echo
the lesser of the two dates, which in this case is $dt1
.
这将echo
是两个日期中的较小者,在本例中为$dt1
。