【问题标题】:Laravel 5 - How to setlocale Carbon time to German [duplicate]Laravel 5 - 如何将地区碳时间设置为德语 [重复]
【发布时间】:2015-08-25 10:48:44
【问题描述】:

我正在使用 Carbon,但我不知道如何将输出更改为德国时间格式。

应该在 Controller 中还是在视图中进行更改?

现在我想要将 DayName 作为 Germanstring。 这是我的默认输出:

{{ $game->start_at }}

当我将视图更改为

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

我得到了 DayName,但不是德语。

【问题讨论】:

    标签: php laravel localization php-carbon


    【解决方案1】:

    也许有人正在寻找将碳日期转换为可读的德语月份:

    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))];
        }
    }
    

    【讨论】:

      【解决方案2】:

      我想你的答案就在这里。遇到麻烦时请记住始终检查文档:D

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

      setlocale(LC_TIME, 'German');
      
      $dt = Carbon::now();
      
      echo $dt->formatLocalized('%A %d %B %Y');
      

      【讨论】:

      【解决方案3】:

      我发现了我的错误,我有

      setlocale(LC_TIME, 'de_DE')
      

      正确的语法是

      setlocale(LC_TIME, 'German');
      

      然后我把它放到 bootstrap/app.php 所以它工作正常。

      【讨论】:

        猜你喜欢
        • 2016-11-08
        • 2019-09-27
        • 1970-01-01
        • 2016-10-09
        • 1970-01-01
        • 1970-01-01
        • 2020-08-20
        • 1970-01-01
        • 2012-07-05
        相关资源
        最近更新 更多