Calculate difference between two dates using Carbon and Blade

前端 未结 4 1621
执念已碎
执念已碎 2020-11-27 20:44

Does anyone know how to pass a given variable instead the Carbon\'s default parameters ?

The documentation of Carbon says:

// CARBON SAMPLE

$dtToron         


        
相关标签:
4条回答
  • 2020-11-27 21:07

    Blade Template

    A shorter code

    {{ $diff = Carbon\Carbon::parse($data->last_updated)->diffForHumans() }}
    .
    

    Result : 6 minutes ago

    0 讨论(0)
  • 2020-11-27 21:08

    Shortest way

    We can directly write it in blade

    <span>{{ \Carbon\Carbon::parse( $start_date )->diffInDays( $end_date ) }}</span>
    
    0 讨论(0)
  • 2020-11-27 21:24

    You code can be cleaned up and have the commented out code removed by doing:

    <td>{{ $diff = Carbon\Carbon::parse($work['date'])->diffForHumans(Carbon\Carbon::now()) }} </td>
    
    0 讨论(0)
  • 2020-11-27 21:31

    You are not following the example from the Carbon Documentation. The method Carbon::createFromDate() expects 4 parameters: year, month, day and timezone. And you are trying to pass a formatted date string.

    If you want to create a Carbon object from a formatted date string you can use the constructor of the class just like this:

    $date = "2016-09-17 11:00:00";
    $datework = new Carbon($date);
    

    Or you can use the static Carbon::parse() method:

    $date = "2016-09-17 11:00:00";
    $datework = Carbon::parse($date);
    

    For your purposes you can use the this full example:

    $date = Carbon::parse('2016-09-17 11:00:00');
    $now = Carbon::now();
    
    $diff = $date->diffInDays($now);
    

    And then in your Blade template:

    <td> {{ $diff }} </td>
    
    0 讨论(0)
提交回复
热议问题