-
Notifications
You must be signed in to change notification settings - Fork 7.8k
IntlDateFormatter returns incorrect week number for an invalid locale (and doesn't throw an exception either) #12282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
…on is the locale field has an invalid value.
…on is the locale field has an invalid value.
IMHO this is a breaking change that should not go into a bugfix release. There might be multiple reasons why you might be using an invalid locale string, e. g. the locale is being taken from a request parameter, is taken from the While I agree that all those should be checked/caught by the surrounding/preceding code, turning this into an exception in a bugfix release is not an option IMHO. Also, I think something that throws an exception should not be added in a RC3 (like 8.3.0RC3), but that's another story. |
If it doesn't throw an exception then I think that it should at least return the correct week number, even if the locale is invalid. The documentation is not very clear, but as far as I can see the 'w' pattern returns the ISO week number. If that's the case then surely the ISO week number is independent of the locale? |
I am afraid it is: |
Maybe you should not expect a particular week number for an invalid locale, given that the week number depends on the locale? Garbage (locale) in, garbage (week no) out, but no exception in a bugfix release. If we want stricter locale validation, that needs to go elsewhere or in more places than just this. |
I agree that the locale could be validated elsewhere, but my point was that if 'w' is the pattern for the ISO week number then in the example above it should give 38 rather than 39 as the ISO week number does not depend on locale (well, at least the value though the character representation may). |
The week number does depend on the locale, see this: <?php
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2016-01-04', new DateTimeZone('Europe/London'));
foreach (['en_US', 'de_DE'] as $locale)
{
$formatter = new IntlDateFormatter(
$locale,
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
null,
null,
'w'
);
echo "Locale: $locale, Week number: " . $formatter->format($date) . "<br>\n";
}
|
Ok, my misunderstanding then. As I said, the documentation isn't very clear. I had assumed that 'w' is the pattern for the ISO week number. If it isn't, do you know what is? |
The "week of year" varies. ISO 8601 has one specification, but that doesn't mean every locale uses it. https://www.unicode.org/reports/tr35/tr35-dates.html#week-of-year
|
Many thanks for the clarifications. I understand the 'w' pattern a lot better now. |
We have update our php version to the lastest yesterday ( Concern line in Symfony core : With inputs :
So it seems than "cz" locale is considerate as an invalid locale ><
So... bug or misconfiguration of our app ? |
The fix (revert of the change) will land in the next 8.1 and 8.2 bugfix release. PHP 8.2.12 was released before the change was made. Caution: PHP 8.3 will bring back the change. |
We pass our users from "cz" locale to "cs" and it works again. Thanx for your response. |
Description
The following code:
Resulted in this output:
But I expected this output instead:
or else for an exception to be thrown when trying to use 'xx'.
This problem happens on all versions of PHP and all operating systems I have tested.
PHP Version
PHP 8.3.0RC2
Operating System
Windows NT 10.0 build 22621 (Windows 11) AMD64
The text was updated successfully, but these errors were encountered: