0

I am using Carbon, and I don't know how to change output to the German Time Format.

Should you make the change in Controller or in the view?

Now I want the DayName as Germanstring. That's my default output:

{{ $game->start_at }}

when I change in view to

{{ $game->start_at->format('l') }}

I get the DayName but not in German.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Wolfgang Müller
  • 386
  • 1
  • 6
  • 21

3 Answers3

1

Maybe someone is looking for converting carbon date into readable german Month:

if ( ! function_exists( 'convert_to_german_month' ) ) {
    /**
     * Converts given Carbon date into German Month
     * Output example: "Januar"
     *
     * @param \Carbon\Carbon $date
     * @return string
     */
    function convert_to_german_month( \Carbon\Carbon $date ) : string {
        $month_mapping = [
            'January' => 'Januar',
            'February' => 'Februar',
            'March' => 'März',
            'April' => 'April',
            'May' => 'Mai',
            'June' => 'Juni',
            'July' => 'Juli',
            'August' => 'August',
            'September' => 'September',
            'October' => 'Oktober',
            'November' => 'November',
            'December' => 'Dezember'
        ];
        return $month_mapping[strftime('%B', strtotime($date))];
    }
}
aurora
  • 9,607
  • 7
  • 36
  • 54
Mathias
  • 67
  • 9
0

Your answer is here I think. Remember to check the documentation always when you are in trouble :D

http://carbon.nesbot.com/docs/#api-localization

setlocale(LC_TIME, 'German');

$dt = Carbon::now();

echo $dt->formatLocalized('%A %d %B %Y');
Chathushka
  • 435
  • 6
  • 13
  • 1
    I know the docs.. where do you put the setlocal() in? In the Controller or view? – Wolfgang Müller Aug 25 '15 at 11:41
  • setlocale() is a php function, when you change the local in php Carbon behaves on that locale. And when you want to revert back, you set locale to default. http://php.net/manual/en/function.setlocale.php – Chathushka Aug 28 '15 at 07:46
  • search for locale in here, you may understand how Carbon uses php localization. https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php – Chathushka Aug 28 '15 at 07:49
0

I have found my error, i had

setlocale(LC_TIME, 'de_DE')

the correct syntax is

setlocale(LC_TIME, 'German');

and I put it to bootstrap/app.php So it´s working.

Wolfgang Müller
  • 386
  • 1
  • 6
  • 21