Laravel Carbon date diffInDays() on string error

后端 未结 6 1063
无人及你
无人及你 2021-01-16 08:23

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_d         


        
6条回答
  •  花落未央
    2021-01-16 08:25

    You can only use the diffInDays() function on a Carbon instance at before date format apply.

    $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 document for further detail.

提交回复
热议问题