laravel Carbon:仅按日期区分两个日期时间对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45359377/
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
Carbon: diff two datetime objects by dates only
提问by lesssugar
Assuming I have the following code:
假设我有以下代码:
$now = Carbon::now();
$dateTimeObject = Carbon::parse('2017-07-20 10:16:34');
how do I get the diffbetween the dates only, ignoring the time factor?
我如何只获得日期diff之间的时间,而忽略时间因素?
So, if $nowis 2017-07-27 09:11:12, and the date in the $dateTimeObjectis 2017-07-20 -- the difference would be 7.
因此,如果$now是 2017-07-27 09:11:12,并且 中的日期$dateTimeObject是 2017-07-20 - 差异将是7。
I need it to make sure the results of a specific operation are being stored in the database only once per day.
我需要它来确保特定操作的结果每天仅在数据库中存储一次。
Note:
笔记:
I tried the diffInDays()method, but it returns 0if the values are e.g. 2016-10-12 23:56:43and 2016-10-13 02:01:53- so, close to midnight and at night.
我尝试了该diffInDays()方法,但0如果值是 eg2016-10-12 23:56:43和2016-10-13 02:01:53- 所以接近午夜和晚上,它会返回。
回答by Perfect Square
Do something like this:
做这样的事情:
$now = Carbon::now()->startOfDay();
$dateTimeObject = Carbon::parse('2017-07-20 10:16:34')->startOfDay();
And now use diffInDays().
现在使用diffInDays().

