Search code examples
phplaravellocalephp-carbon

How to get German day name from Carbon date?


The German local is installed on my server, I checked with locale -a

enter image description here

This is what I tried:

setlocale(LC_ALL, 'de_DE') or die('Locale not installed');
dd($user->created_at->format('l'));

it shows "Monday". However, the German word for this day should be "Montag".

I also tried

setlocale(LC_ALL, 'de_DE') or die('Locale not installed');
\Carbon\Carbon::setLocale('de_DE'); 
dd($user->created_at->format('l'));

but its still "Monday" instead of "Montag".

What am I missing?


Solution

  • If you are using Carbon 1, use like the following

    $newLocale = setlocale(LC_TIME, 'German');
    $dt = Carbon::parse('1975-05-21 22:23:00.123456');
    if ($newLocale === false) {
        echo '"German" locale is not installed on your machine, it may have a different name on your machine or you may need to install it.';
    }
    echo $dt->formatLocalized('%A %d %B %Y');          // Mittwoch 21 Mai 1975
    setlocale(LC_TIME, 'English');
    echo $dt->formatLocalized('%A %d %B %Y');          // Wednesday 21 May 1975
    setlocale(LC_TIME, ''); // reset locale
    

    doc link