Carbon: diff two datetime objects by dates only

折月煮酒 提交于 2019-12-01 04:24:23

问题


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?

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.

Note:

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.


回答1:


Do something like this:

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

And now use diffInDays().



来源:https://stackoverflow.com/questions/45359377/carbon-diff-two-datetime-objects-by-dates-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!