Laravel Carbon date diffInDays() on string error

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

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2021-01-16 08:33

    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)).

    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()
    
    0 讨论(0)
  • 2021-01-16 08:42

    Not tested but try this:

    $formatted_dt1=Carbon::parse($a->date);
    $formatted_dt2=Carbon::parse($c->dt);
    $date_diff=$formatted_dt1->diffInDays($formatted_dt2);
    
    0 讨论(0)
  • 2021-01-16 08:44

    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.

    0 讨论(0)
提交回复
热议问题