KiritoLyn
KiritoLyn

Reputation: 760

Format Date value from database Laravel

I have a column named birth_date in MySQL database with a type of date.

enter image description here

Now, I would like to show it in my view in this format : May 23, 2008

I tried:

{{ $user->user_information->birth_date->format('m-d-Y') }}

it showed:

ErrorException (E_ERROR) Call to a member function format() on string

I also tried

{{ date('m-d-Y', $user->user_information->baptism_date) }}

it showed:

ErrorException (E_ERROR) A non well formed numeric value encountered

Can anyone assist me with the right formatting code?

Upvotes: 0

Views: 121

Answers (3)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

You can use the date() function for this purpose like:

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

or you can also use Carbon for this purpose like:

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

For May 23, 2008:

date('M d, Y', strtotime($user->user_information->birth_date));

Working code snippet

date() format list is available here

M - A short textual representation of a month, three letters

Upvotes: 1

Bhoomi Patel
Bhoomi Patel

Reputation: 775

you can use date function. try this code:

{{ date('F j, Y', strtotime($user->user_information->birth_date)); }}

Upvotes: 1

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

Try this code.

{{ Carbon\Carbon::parse($user->user_information->birth_date)->format('F d, Y') }}

Upvotes: 1

Related Questions