php 如何添加 1 小时至今碳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40787699/
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 add 1 hour to date Carbon?
提问by Goga
I have datetime
"2016-11-24 11:59:56"
. How can I add one hour to this date and compare it with current datetime?
我有datetime
"2016-11-24 11:59:56"
。如何在此日期上添加一小时并将其与当前日期时间进行比较?
I tried:
我试过:
$date = "2016-11-24 11:59:56";
$date->addHour();
回答by Alexey Mezenin
Try to parse()
it first:
先试试看parse()
:
$date = Carbon::parse('2016-11-24 11:59:56')->addHour();
Better way is to add datetime column to a dates
variable:
更好的方法是将日期时间列添加到dates
变量中:
protected $dates = ['datetime_column'];
Then you can just do this:
然后你可以这样做:
$date = $model->datetime_column->addHour();
回答by Saumya Rastogi
You can try this:
你可以试试这个:
$date = "2016-11-24 11:59:56";
$carbon_date = Carbon::parse($date);
$carbon_date->addHours(1);
You can now compare the date using lt()
& gt()
methods of Carbon. See Carbon Comparison Docsfor exploring more methods like - eq()
, ne()
, etc.
您现在可以使用Carbon 的lt()
>()
方法比较日期。见碳比较文档探索像更多的方法- eq()
,ne()
等等。
$carbon_date->lt(Carbon::now()); // Less then current datetime (returns boolean)
$carbon_date->lt(Carbon::now()); // Greater then current datetime (returns boolean)
Hope this helps!
希望这可以帮助!