简体   繁体   English

Carbon:仅按日期区分两个日期时间对象

[英]Carbon: diff two datetime objects by dates only

Assuming I have the following code:假设我有以下代码:

$now = Carbon::now();
$dateTimeObject = Carbon::parse('2017-07-20 10:16:34');

how do I get the diff between the dates only , ignoring the time factor?如何获取日期之间的diff ,而忽略时间因素?

So, if $now is 2017-07-27 09:11:12, and the date in the $dateTimeObject is 2017-07-20 -- the difference would be 7 .因此,如果$now是 2017-07-27 09:11:12,并且$dateTimeObject中的$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 0 if the values are eg 2016-10-12 23:56:43 and 2016-10-13 02:01:53 - so, close to midnight and at night.我尝试了diffInDays()方法,但如果值是例如2016-10-12 23:56:432016-10-13 02:01:53 ,它返回0 - 所以,接近午夜和晚上。

Do something like this:做这样的事情:

$now = Carbon::now()->startOfDay();
$dateTimeObject = Carbon::parse('2017-07-20 10:16:34')->startOfDay();

And now use diffInDays() .现在使用diffInDays()

Carbon::isSameDay method will check if the given date is in the same day as the instance. Carbon::isSameDay 方法将检查给定日期是否与实例在同一天。 If null passed, compare to now (with the same timezone).如果 null 传递,则与现在进行比较(具有相同的时区)。

$dateTimeObject = Carbon::parse('2017-07-20 10:16:34');

$is_same = $dateTimeObject->isSameDay();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM