How do I get the difference of time in human readable format with Laravel 5?

前端 未结 2 509
梦毁少年i
梦毁少年i 2020-12-30 20:48

I want to display the difference between the current date and time with the one stored in the updated_at column. However, I want it to be human-friendly like:

相关标签:
2条回答
  • 2020-12-30 21:32

    By default, Eloquent converts created_at and updated_at columns to instances of Carbon. So if you are fetching the data using Eloquent, then you can do it as below.

    $object->updated_at->diffForHumans();
    

    If you want to customize the fields that will be mutated automatically, then within your model, you can customize them as you wish.

    // Carbon instance fields
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];
    
    0 讨论(0)
  • 2020-12-30 21:40

    incase of the date is in a string format, use laravel Carbon; e.g,

     {{ \Carbon\Carbon::parse($on_revision->assignment->deadline)->diffForhumans() }}
    

    Notice how I wrap my string in the carbon::parse()

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