Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rjsworking's avatar

Carbon - Localized date

Hi all

I have a model with a timestamp field named start_at. The date was displayed as yyyy-mm-dd hh:mm:ss but now I need to print the data in a pdf file and it should be formatted like "Sunday 8 February 2015" in English, Spanish and Portuguese.

I'm doing this develoment in a Homestrad VM machine.

In the model I have

protected $dates = array('start_at');

Added missing locales in system and checked they are available

locale -a returns

C
C.UTF-8
en_US.utf8
es_ES.uft8
POSIX
pt_BR.utf8
pt_PT.iso88591
pt_PT.utf8

In /etc/default/locale tried 
LANG="es_ES.UTF-8", LANG="es_ES.utf8"
LC_ALL="es_ES.UTF-8", LC_ALL="es_ES.utf8"

In the view:

$info->start_at->formatLocalized('%A %d %B %Y');

This is a L4 app and in config\app.php I tried everything I know

'locale' => 'es_ES.UTF-8', 
'locale' => 'es_ES.utf8',

It always return the date localized in English and I can't figure out a way to localize the date othe than English.

Thanks in advance.

0 likes
7 replies
JarekTkaczyk's avatar

@rsdev000 This will work for you, once you've added the locale:

setlocale(LC_TIME, 'es_ES.utf8');
>>> Carbon\Carbon::now()->formatLocalized('%A %d %B %Y');
=> "domingo 08 febrero 2015"
2 likes
rjsworking's avatar

Thanks. Didn't try your solution before but I need to localize a date that comes from the database.

JarekTkaczyk's avatar

@rsdev000 Yes, I know. It doesn't matter, that was just an example. Feel free to run your $info->start_at->formatLocalized('%A %d %B %Y'); and it will stil work.

rjsworking's avatar

That is what I've got in the view as my first post and it doesn't work. Always prints the date in english.

Tricky issue.

rjsworking's avatar

Hi @JarekTkaczyk

Yes I am. Tested again yesterday. It works but the app uses a localization package and I will need to write a route filter to map the package locales to the system locales in order to avoid urls like example.com/es_ES.utf8/contacto and keep the pretty one - example.com/es/contacto

Thanks

kornel's avatar

Nowadays it is bettet to use ->isoFormat('dddd DD MMMM YYYY') instead of ->formatLocalized('%A %d %B %Y') because formatLocalized() is deprecated

Please or to participate in this conversation.