I’ve a problem in formatting dates in it_IT locale.
I’ve a customer with a site running on PHP 8.0.3, and due to some certification issues (this customer has to mantain ISO27001 certification) I’ve been asked to port the PHP engine to the lastest available version (today is 8.3.9). This site runs on IIS on a Windows Server 2022 machine, but I’ve the same problem on my machine running Apache inside Xampp 3.3.0
I’ve built a simple page to reproduce the issue, and this is the content:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
setlocale(LC_ALL, 'it.UTF8','IT.UTF8','it_IT.UTF8');
echo '<h3>PHP version : '.phpversion().'</h3><br>'.PHP_EOL;
$dt = IntlDateFormatter::formatObject(new DateTime('now', new DateTimeZone('Europe/Rome')),IntlDateFormatter::FULL,'it');
echo 'IntlDateFormatter::formatObject(new DateTime('now', new DateTimeZone('Europe/Rome')),IntlDateFormatter::FULL,'it')'.PHP_EOL;
echo '<h3>'.$dt.'</h3>'.PHP_EOL;
$dt = strftime('%e %B %Y, %A');
echo 'strftime('%e %B %Y, %A')'.PHP_EOL;
echo '<h3>'.$dt.'</h3>'.PHP_EOL;
$formatter = new IntlDateFormatter('it_IT', IntlDateFormatter::FULL, IntlDateFormatter::FULL);
echo '$formatter = new IntlDateFormatter('it_IT', IntlDateFormatter::FULL, IntlDateFormatter::FULL); $formatter->format(new DateTime('now'));'.PHP_EOL;
echo '<h3>'.$formatter->format(new DateTime('now')).'</h3>'.PHP_EOL;
?>
</body>
</html>
This is the resulting page, taken from Firefox DevTools:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h3>PHP version : 8.3.9</h3><br>
IntlDateFormatter::formatObject(new DateTime('now', new DateTimeZone('Europe/Rome')),IntlDateFormatter::FULL,'it')
<h3>giovedì 25 luglio 2024 alle ore 11:48:58 Ora legale dell’Europa centrale</h3>
strftime('%e %B %Y, %A')
<h3>25 luglio 2024, giovedì</h3>
$formatter = new IntlDateFormatter('it_IT', IntlDateFormatter::FULL, IntlDateFormatter::FULL); $formatter->format(new DateTime('now'));
<h3>giovedì 25 luglio 2024 alle ore 11:48:58 Ora legale dell’Europa centrale</h3>
</body>
</html>
I’ve used basically two different methods: the first uses the deprecated strftime that works on PHP 8.0.3 (and makes IIS on PHP 8.3.9 return a 500 error instead of the annoying but simpler deprecation notice returned by Apache), and two flavours of the new class IntlDateFormatter.
Searching on the web gives absolutely no usable advices, and most of the times the more you search the less you obtain, so I’m asking you, the Community, if anyone has experienced the same issue. For now the customer simply ignores the strange letters, but I’ve assured him I’ll come ASAP with the solution…
1