Formatting Timestamps in Laravel

Database Views

Committed to the LaraBrain by: wjgilmore (@wjgilmore) at November 11, 2015 2:27 pm

Laravel provides an easy but little-mentioned way to format timestamps

The created_at and updated_at timestamp columns typically added to models' associated database tables are useful for informing users about when a particular record was created or last updated, respectively. For instance you might want to display information about when the authenticated user created his account:

{{ Auth::user()->created_at }}

This however produces an unfriendly date/time format which looks like this:

2015-12-01 13:45:12

Laravel offers a much more user friendly alternative. You can use the format method, accompanied by one or more of the same formatting options supported by PHP's date() function:

{{ Auth::user()->created_at->format('F d, Y') }}

Output to the browser, the timestamp will now look like this:

December 01, 2015