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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:14:58  来源:igfitidea点击:

How to add 1 hour to date Carbon?

phplaravellaravel-5.3php-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 datesvariable:

更好的方法是将日期时间列添加到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()&gt()方法比较日期。见碳比较文档探索像更多的方法- 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!

希望这可以帮助!