Coder Perfect

In the Laravel view page, update the date format.

Problem

I’d like to change the date format that the database returns. Now I have 2016-10-01$user->from date as a result. In Laravel 5.3, I’d like to alter the format to ‘d-m-y’.

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

Asked by user3386779

Solution #1

Try this:

date('d-m-Y', strtotime($user->from_date));

It will convert the date to d-m-Y or any other format you specify.

Note: This is a generic solution that will work with PHP and its frameworks. Consider Hamelraj’s approach for a Laravel-specific technique.

Answered by Mayank Pandeyz

Solution #2

It’s an excellent idea to use Carbon in Laravel.

{{ \Carbon\Carbon::parse($user->from_date)->format('d/m/Y')}}

Answered by Hamelraj

Solution #3

In your Model folder, you’ll find:

protected $dates = ['name_field'];

after that, in your opinion:

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

works

Answered by HorecioDias

Solution #4

Date Mutators may be found at https://laravel.com/docs/5.3/eloquent-mutators#date-mutators.

You must first set the from date column in the $dates array in your User model, and then modify the format in $dateFormat.

Another possibility is to add this method to your User model:

public function getFromDateAttribute($value) {
    return \Carbon\Carbon::parse($value)->format('d-m-Y');
}

After that, if you run $user->from date in view, you’ll get the format you want.

Answered by Marek Skiba

Solution #5

Carbon is a simple way to incorporate a date in a blade template.

{{ \Carbon\Carbon::parse($user->from_date)->format('d/m/Y')}}

Answered by Umar Tariq

Post is based on https://stackoverflow.com/questions/40038521/change-the-date-format-in-laravel-view-page