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

dkBITS's avatar
Level 13

L9 new Accessor DateTime Locale Format

Hi Everybody,

i am trying to return a date from my database with the new accessor syntax

protected function jobPublishingDateFrom(): Attribute { return new Attribute( get: fn ($value) => (Carbon::parse($value))->format('d.F Y'), ); }

In the config/app.php the Timezone is configured to 'Europe/Berlin' and the APP_LOCALE=de but the name of the day is everytime in english. I tried to set ->locale('de') for carbon manually but nothing changed.

Casting like this also all the time english:

protected $casts = [ 'job_publishing_date_from' => 'datetime:d.F Y' ];

Any Ideas?

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You can use the translatedFormat method:

protected function jobPublishingDateFrom(): Attribute
{
    return new Attribute(
        get: fn ($value) => (Carbon::parse($value)->translatedFormat('d.F Y'),
    );
}
1 like
dkBITS's avatar
Level 13

@tykus Very cool. Why did I miss this in the doc? Thanks

Every thread i found and so far i know until today, everbody is struggeling around with: Carbon::setLocale('de'); before outputting the formatted value.

Please or to participate in this conversation.