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:

now()->format('F');

// 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!

now()->formatLocalized('%B');

// result : Janvier

about me

profile picture

I consider myself as an IT Business Artisan. Or Consultant CTO. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.

more about me

follow me

newsletter

A weekly email with the latests articles

support my work

Start trading on binance
  • BTC

    BTC

    bc1qgw9a8hyqqwvcls9ln7vhql4mad0qkveutr2td7

  • ETH

    ETH

    0x3A720717Da03dB6f3c4a4Ff08BC64100205A79d0

2025 © My Dynamic Production SRL All rights Reserved.