Heril Muratovic
Heril Muratovic

Reputation: 2018

Format Date with Laravel Carbon

How to format date YYYY-MM-DD to, for example, to format 21, April 2015. with Carbon package?

I have already convert YYYY-MM-DD to DD-MM-YYYY, but I can't convert it to specified format.

Does anyone have an idea hot to do it?

Something like: Carbon::createFromFormat(...)

Upvotes: 0

Views: 5941

Answers (2)

Eduardo Quiñonez
Eduardo Quiñonez

Reputation: 119

You can use FormatLocalized, like the example above, to do that you have to configure your AppServiceProvider file, and put the language (locale = 'en')that you want to output all your instances of Carbon Class.

NOTE: if you want to output dates in a blade view, you could declare the date fields in your model to convert them in Carbon instances, and not declare them in the view. example:

output in blade:

{{ Carbon\Carbon::parse($user->date1)->formatLocalized('%d, %B %Y')}}

If you want to omit that, use this in your model:

protected $dates = [
  'date1',
  'date2',
  'date3'
];

now, you can output in the views the date fields without to call Class

{{ ($user->date1)->formatLocalized('%d, %B %Y') }}
{{ ($user->date2)->formatLocalized('%d, %B %Y') }}
{{ ($user->date3)->formatLocalized('%d, %B %Y') }}

Upvotes: 0

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

you can try like this,

echo $dt->formatLocalized('%d, %B %Y'); 

Be careful about cases

See Localization and string formatting for more info

Upvotes: 1

Related Questions