Laravel Carbon 日期 diffInDays() 关于字符串错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45858410/
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
Laravel Carbon date diffInDays() on string error
提问by Muthu
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
我需要找到两个日期之间的差异。假设我有 2017-02-01 - 2017-01-01。两天之间的天数就是输出
$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
回答by Sagar Gautam
Carbon format()
function will convert to string so remove format('Y-m-d')
like this:
Carbonformat()
函数将转换为字符串,因此删除format('Y-m-d')
如下:
$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.
希望你能理解。您可以在此处查看文档。
回答by Murwa
Not tested but try this:
未测试,但试试这个:
$formatted_dt1=Carbon::parse($a->date);
$formatted_dt2=Carbon::parse($c->dt);
$date_diff=$formatted_dt1->diffInDays($formatted_dt2);
回答by Rahul
You can do in this way,
你可以这样做,
$formatted_dt1=Carbon::parse($a->date);
$formatted_dt2=Carbon::parse($c->dt);
$date_diff=$formatted_dt1->diffInDays($formatted_dt2)->format('Y-m-d');
First get the difference from two dates and then format the date.
首先获取两个日期的差异,然后格式化日期。
回答by AddWeb Solution Pvt Ltd
You can only use the diffInDays()
function on a Carbon instance at before date format apply.
您只能diffInDays()
在应用日期格式之前在 Carbon 实例上使用该函数。
$formatted_dt1=Carbon::parse($a->date);
$formatted_dt2=Carbon::parse($c->dt);
Now you should be able to compare:
现在你应该能够比较:
$date_diff=$formatted_dt1->diffInDays($formatted_dt2);
if you want to apply date format, try below for compare them:
如果您想应用日期格式,请尝试在下面进行比较:
$date_diff=$formatted_dt1->diffInDays($formatted_dt2)->format('Y-m-d');
Check this documentfor further detail.
查看此文档以获取更多详细信息。
回答by Austin
You are having this problem because you stored your date as string in the database. You can perform diffInDays($updated_at)
or diffInDays($created_at)
on the original laravel's created_at
and updated_at
because Laravel already stores them as dates by default so, when you are fetching them from the database, laravel gives it to you as a carborn instance (try dd($created_at)
).
您遇到此问题是因为您将日期作为字符串存储在数据库中。您可以在原始 Laravel 上执行diffInDays($updated_at)
或并且因为 Laravel 默认情况下已经将它们存储为日期,所以当您从数据库中获取它们时,laravel 将其作为 carborn 实例提供给您(尝试)。diffInDays($created_at)
created_at
updated_at
dd($created_at)
To solve your problem, go to your model and use this to convert your date column to dates
要解决您的问题,请转到您的模型并使用它来将您的日期列转换为日期
protected $dates = [
'my_date',
'my_other_date'
]
Then, you can now do
那么,你现在可以做
$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);
OR You can use
或者你可以使用
Carbon\Carbon::parse($formatted_dt1)->diffInDays()
回答by Dilip Hirapara
Make sure to use Carbon\Carbon;
确保 use Carbon\Carbon;
- You can use
diffInDays
for days. - You can use
diffInHours
for Hours. - You can use
diffInMinutes
for Minutes. - You can use
DiffInSeconds
for Seconds.
- 您可以使用
diffInDays
的天。 - 您可以使用
diffInHours
的时间。 - 您可以使用
diffInMinutes
的分钟。 - 您可以使用
DiffInSeconds
为秒。
$formatted_dt1=Carbon::parse('2019-09-26 00:00:00');
$formatted_dt2=Carbon::parse('2019-09-28 00:00:00');
$date_diff=$formatted_dt1->diffInDays($formatted_dt2);
echo $date_diff.' Day '; //2 days
$hours_diff = $formatted_dt1->diffInHours($formatted_dt2);
echo $date_diff.' Hours '; //48 Hours
$Minutesdiff = $formatted_dt1->diffInMinutes($formatted_dt2);
echo $Minutesdiff.' Minutes '; //2880 Minutes
$seconddiff = $formatted_dt1->DiffInSeconds($formatted_dt2);
echo $seconddiff.' Seconds '; //172800 Seconds
exit;