Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble displaying created_at value in view

I'm trying to display the created_at column in one of my views and I'm not having any luck. When I try using the below:

{{ date('d M Y - H:i:s', $object->created_at) }}

I get the following error: date() expects parameter 2 to be long, object given. So I use dd() on $object->created_at and it gives me this:

object(Carbon\Carbon)#555 (3) { ["date"]=> string(26) "2015-01-22 14:36:37.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "Africa/Johannesburg" }

So naturally I tried accessing the date with a foreach loop which didn't work and I also tried $object->created_at->date which gave me an error "Unknown getter 'date'" How do I access the date from created_at?

like image 973
David Ross Avatar asked Feb 03 '15 02:02

David Ross


2 Answers

Laravel uses Carbon. Then try this:

{{ date('d M Y - H:i:s', $object->created_at->timestamp) }}

Or this is the same without date function

{{ $object->created_at->format('d M Y - H:i:s') }}

Or use any method documented in the Carbon API

like image 163
Edgar Orozco Avatar answered Nov 02 '22 17:11

Edgar Orozco


This is another method -

{{ date('F d, Y', strtotime($list->created_at)) }}
like image 4
Hasan Hafiz Pasha Avatar answered Nov 02 '22 17:11

Hasan Hafiz Pasha