change the date format in laravel view page

前端 未结 11 2086
-上瘾入骨i
-上瘾入骨i 2020-12-02 06:23

I want to change the date format which is fetched from database. now I got 2016-10-01{{$user->from_date}} .I want to change the format \'d-m-y\' in laravel

相关标签:
11条回答
  • 2020-12-02 06:53

    Easy to use date in blade template use Carbon that way

    {{ \Carbon\Carbon::parse($user->from_date)->format('d/m/Y')}}
    
    0 讨论(0)
  • 2020-12-02 06:53

    Sometimes changing the date format doesn't work properly, especially in Laravel. So in that case, it's better to use:

    $date1 = strtr($_REQUEST['date'], '/', '-');
    echo date('Y-m-d', strtotime($date1));
    

    Then you can avoid error like "1970-01-01"!

    0 讨论(0)
  • 2020-12-02 06:54

    In Laravel use Carbon its good

    {{ \Carbon\Carbon::parse($user->from_date)->format('d/m/Y')}}
    
    0 讨论(0)
  • 2020-12-02 06:58

    You can use Carbon::createFromTimestamp

    BLADE

    {{ \Carbon\Carbon::createFromTimestamp(strtotime($user->from_date))->format('d-m-Y')}}
    
    0 讨论(0)
  • 2020-12-02 07:00

    In your Model set:

    protected $dates = ['name_field'];
    

    after in your view :

    {{ $user->from_date->format('d/m/Y') }}
    

    works

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