在 PHP 中始终获取德语月份名称

getting always german month-names in PHP

我的PHP-代码

$now = new \DateTime();
echo $now->format('d. M.')

我得到的

12. Dec.(英文)

要我要

12. Dez.(德语)

我目前的解决方案

$formatter = new \IntlDateFormatter(
                    "de-DE",
                    \IntlDateFormatter::FULL,
                    \IntlDateFormatter::NONE,
                    "Europe/Berlin",
                    \IntlDateFormatter::GREGORIAN,
                    "dd. MMM"
                    );

echo $formatter->format($now);

问题

总是$formatter创造有点沉重。 在使用 $now->format('d. M.') 时调用“月份”时,是否可以更改 php.ini(或类似)中的某些内容以始终获得德语单词?

我已经在 php.ini 中尝试过这个(但没有帮助): intl.default_locale = de

副本:change month name to french

From http://php.net/manual/en/function.date.php:

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

我打算使用它,您可以在其中创建自己的 class,它会在构造函数中完成所有工作,并且只提供一个简单易用的功能(但仍然可以灵活地更改模式)

class Formatter {
    private $dateFormatter;

    public function __construct() {
        $formatter = new \IntlDateFormatter(
            "de-DE",
            \IntlDateFormatter::MEDIUM,
            \IntlDateFormatter::MEDIUM,
            "Europe/Berlin");
        $this->dateFormatter = $formatter;
    }

    public function printDate(\DateTime $dateTime, string $pattern = null) {
        if ($pattern) {
            $this->dateFormatter->setPattern($pattern);
        }

        return $this->dateFormatter->format($dateTime);
    }
}

用法

$fmt = new Formatter();
echo $fmt->printDate($now, "d. MMM");