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

johnnw's avatar

translate carbon date

I'm showing the date using carbon like this:


{{$p->date->toDayDateTimeString()}}

But I would like to show the date text in a different language "de".

So I changed:


    'locale' => 'en'                       to     'locale' => 'de'
     'fallback_locale' => 'en',     to     'fallback_locale' => 'de'

But don't works.

I also try to in the "AppServiceProvider" add in the boot method:


 public function boot()
    {
        Carbon::setLocale(env('LOCALE', 'de'));
    }

But also don't works.

Do you know why?

0 likes
4 replies
Cronix's avatar
Cronix
Best Answer
Level 67

The only thing that should be using env() is your config files. If you cache the config, which you should do in production, it will ignore env totally and only pull values from config.

but for one thing, it's locale in your config (lowercase), but you're trying to reference it with LOCALE uppercase.

Carbon::setLocale(config('app.locale'));
1 like
johnnw's avatar

Thanks, but even with 'locale' instead of 'LOCALE' on the "AppServiceProvider" boot method don't translates. With that "Carbon::setLocale(config('app.locale'));" the 'de' needs to be put where?

skliche's avatar

toDayDateTimeString() does not seem to be localized. The methods diffForHumans() and formatLocalized() are.

But diffForHumans() needs a call like Carbon::setLocale('de'); and formatLocalized() needs a call like setlocale(LC_TIME, 'de_DE'); to work.

So you might want to save both the language and the language with the region in your config:

'locale' => env('APP_LOCALE', 'de'),
'localeWithRegion' => env('APP_LOCALE_REGION', 'de_DE'),

Then you can use ...

Carbon::setLocale(config('app.locale'));
setlocale(LC_TIME, config('app.localeWithRegion'));

... in AppServiceProvider.

3 likes
sword-fish's avatar

you may also need to add 'translatedFormat' apart from Carbon::setLocale('...'); like so:

Carbon::setLocale('ro');
Carbon::parse($this->created_at)->translatedFormat('d F, Y')
2 likes

Please or to participate in this conversation.