PHP Laravel change date format example

Sometime, we may require to change date format in laravel view file or controller, so you can change your date format using carbon, date, strtotime. Here i gave you three example of change dateformat of timestamp column.

In this example i have simple users table with id, name, email, created_at and updated_at column. In created_at column we store timestamp value like "Y-m-d H:i:s" and here i will convert this format into "d/m/Y". So i did that using three way as like bellow example:

1) Using Laravel Model

$user = \App\User::find(1);

$newDateFormat = $user->created_at->format('d/m/Y');


2) Using PHP strtotime

$user = \App\User::find(1);

$newDateFormat2 = date('d/m/Y', strtotime($user->created_at));


3) Using Carbon

$user = \App\User::find(1);

$newDateFormat3 = \Carbon\Carbon::parse($user->created_at)->format('d/m/Y');


I hope you found your best solution....

July 9, 2017 | 1152 Viewer | Category : PHP Laravel

Related Posts: