1

My PHP-code

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

What I get

12. Dec. (in English)

Want I want

12. Dez. (in German)

My current solution

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

echo $formatter->format($now);

Question

Creating alway $formatter is a little bit heavy. Isn't it possible to change something in php.ini (or similar) to get always the german words when calling the "month" when using $now->format('d. M.')?

I tried already in php.ini this (but it didn't helped): intl.default_locale = de

Tim K.
  • 335
  • 4
  • 23
  • 3
    Does this answer your question? [PHP date - get name of the months in local language](https://stackoverflow.com/questions/13845554/php-date-get-name-of-the-months-in-local-language) – wizardofOz Jun 28 '20 at 08:53
  • Your approach is fine. – Mike Doe Jun 28 '20 at 09:04
  • Er... You don't need to create a formatter every time you manipulate a date. A single formatter instance should be enough for the complete application. – Álvaro González Jun 28 '20 at 10:13
  • Thats true, but for every other "format" i need an additional one. My question was more if there is a general way to make PHP more international, without saying each time, that i want to have the month/weekday not in english – Tim K. Jun 28 '20 at 11:19

2 Answers2

0

Duplicate of: 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().

Raffobaffo
  • 2,806
  • 9
  • 22
  • thanks, but your answer is just giving me another "workaround" (with timestamps) for my already existing "workaround" (with DateTime). My question was more to know if this cannot be solved in generell way. – Tim K. Jun 28 '20 at 10:18
  • Thats no workaround, that is how you do it properly. – Raffobaffo Jun 28 '20 at 10:48
  • That depends on point of view how you call it. anyway: your answer is not fitting to my question. – Tim K. Jun 28 '20 at 11:17
0

I popose to use this, where you create an own class, that does all the work in the constructor and offers only one simple-to-use function (but still felxible to change the patten)

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

usage

$fmt = new Formatter();
echo $fmt->printDate($now, "d. MMM");
Tim K.
  • 335
  • 4
  • 23