Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Carbon date diffInDays() on string error

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
like image 914
Muthu Avatar asked Oct 15 '25 23:10

Muthu


1 Answers

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.

like image 96
Sagar Gautam Avatar answered Oct 17 '25 13:10

Sagar Gautam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!