Home > Software engineering >  strftime in PHP 8.1
strftime in PHP 8.1

Time:11-19

I'm slowly moving all my sites from PHP 7.4 to PHP 8.1 and one of the issues I'm facing is this piece of code:

setlocale(LC_TIME, array('nl_NL.UTF-8', 'nl_NL@euro', 'nl_NL', 'dutch'));
echo ucfirst(strftime('%B', mktime(0, 0, 0, 1, 1, 2022)));

I found the following, but how get it to show only the month?

$formatter = new IntlDateFormatter('nl_NL', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
echo $formatter->format(time());

CodePudding user response:

The IntlDateFormatter can accept a special format for the output as a $pattern parameter.

$dateTime = new DateTime('2022-05-17');

echo \IntlDateFormatter::create('nl_NL', IntlDateFormatter::NONE, IntlDateFormatter::NONE, null, null, 'MMMM')
      ->format($dateTime);  //mei

Try self : https://3v4l.org/IaPnM

The short form of the month has the format 'MMM'. In addition to an integer timestamp, the format method also accepts other objects such as DateTime.

For simple cases, the formatLanguage function can also be used, which accepts the well-known Date/DateTime formats.

  • Related