How To Localize Formatted Dates with Laravel And Carbon

profile picture

How To Localize Formatted Dates with Laravel And Carbon

It's the second time in my carreer as a Laravel Developer that I had to struggle against this, so I decided to write the solution down for my future self πŸ˜…

First of all, here's what we are trying to achieve:


// expected : Janvier (french)

// result : January

The first thing to know is that when you're using ->format(), Carbon actually uses default PHP date formating, so you must tell PHP to use an available locale from the system.

To list all current locales on your system, simply type locale -a in your terminal.

To tell PHP to use on the avilable locale, you can simply add this line in your AppServiceProvider:

public function boot()
    setlocale(LC_TIME, 'fr_FR', 'fr', 'FR', 'French', 'fr_FR.UTF-8');
    Carbon::setLocale('fr'); // This is only needed to use ->diffForHumans()

Next, be careful about the ->format() method. It won't handle the localization. Instead, you must use ->formatLocalized().

BUT if you use ->formatLocalized(), you must use other parsing letters that are listed here.

And that's it!


// result : Janvier

2021 My Dynamic Production SPRL All rights Reserved.