Laravel Carbon日期diffInDays()出现字符串错误 [英] Laravel Carbon date diffInDays() on string error

查看:637
本文介绍了Laravel Carbon日期diffInDays()出现字符串错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到两个日期之间的差异.说我有2017-02-02-2017-01-01.两天之间的天数就是输出

I need to find the difference between the two dates. Say i have 2017-02-01 - 2017-01-01. The number of days between the two days is the output

$formatted_dt1=Carbon::parse($a->date)->format('Y-m-d');
$formatted_dt2=Carbon::parse($c->dt)->format('Y-m-d');
$date_diff=$formatted_dt1->diffInDays($formatted_dt2);

如果我给出上面的代码,我将得到错误消息

If I give the above code I get the error as

FatalThrowableError in ReportsController.php line 67:
Call to a member function diffInDays() on string

推荐答案

Carbon format()函数将转换为字符串,因此像这样删除format('Y-m-d'):

Carbon format() function will convert to string so remove format('Y-m-d') like this:

$formatted_dt1=Carbon::parse($a->date);

$formatted_dt2=Carbon::parse($c->dt);

$date_diff=$formatted_dt1->diffInDays($formatted_dt2);

希望您能理解.您可以在此处看到文档.

Hope you understand. You can see docs here.

这篇关于Laravel Carbon日期diffInDays()出现字符串错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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