Carbon:仅按日期区分两个日期时间对象 [英] Carbon: diff two datetime objects by dates only

查看:156
本文介绍了Carbon:仅按日期区分两个日期时间对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码:

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

如何忽略时间因素而仅在日期之间获得diff?

how do I get the diff between the dates only, ignoring the time factor?

因此,如果$now是2017-07-27 09:11:12,而$dateTimeObject中的日期是2017-07-20,则差异为7.

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.

我需要确保每天仅一次将特定操作的结果存储在数据库中.

I need it to make sure the results of a specific operation are being stored in the database only once per day.

注意:

我尝试了diffInDays()方法,但是如果值是例如,它将返回0. 2016-10-12 23:56:432016-10-13 02:01:53-因此,接近午夜和晚上.

I tried the diffInDays() method, but it returns 0 if the values are e.g. 2016-10-12 23:56:43 and 2016-10-13 02:01:53 - so, close to midnight and at night.

推荐答案

执行以下操作:

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

现在使用diffInDays().

这篇关于Carbon:仅按日期区分两个日期时间对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆