0

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());
Progman
  • 16,827
  • 6
  • 33
  • 48
vespino
  • 1,714
  • 3
  • 15
  • 28
  • Does this answer your question? [PHP 8.1: strftime() is deprecated](https://stackoverflow.com/questions/70930824/php-8-1-strftime-is-deprecated) – jspit Nov 18 '22 at 16:33
  • Use the pattern parameter: $formatter = new IntlDateFormatter('nl_NL', IntlDateFormatter::LONG, IntlDateFormatter::NONE,null,null,'MMMM'); – jspit Nov 18 '22 at 16:46
  • @jspit I would upvote that second comment if you wrote it as an answer with a link to the docs detailing that parameter – IMSoP Nov 18 '22 at 17:18

1 Answers1

1

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.

jspit
  • 7,276
  • 1
  • 9
  • 17