Laravel Carbon date diffInDays() on string error

后端 未结 6 1064
无人及你
无人及你 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

    Make sure to 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.

        $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;
    

提交回复
热议问题