Home > Software engineering >  Get Locale Date in PHP - 2022
Get Locale Date in PHP - 2022

Time:08-27

How should I get the Day and Month (i.e. Friday, August) with the corresponding locale (i.e. de_DE) in PHP?

Most examples use setlocale() and strftime(), the last one being deprecated as of PHP 8.1.0.

CodePudding user response:

You are correct about strftime() being deprecated, and date() (being a part of DateTimeInterface) will not work with setlocale(). One thing that will work with it is IntlDateFormatter:

$fmt = new IntlDateFormatter('de_DE',
    IntlDateFormatter::FULL, 
    IntlDateFormatter::FULL
);

echo $fmt->format(time());

Will output:

Samstag, 27. August 2022 00:58:15 GMT

You can play around with the Predefined Constants to specify different formats for the DateType and TimeType. I used FULL for my example. Read more here.

You can also change the Pattern

$fmt->setPattern('yyyymmdd hh:mm:ss z');
echo $fmt->format(time());

To output (for example):

20220427 01:04:51 GMT

  • Related