admin管理员组文章数量:1123598
At the end of each month i want to write down the month in serbian latin alphabeth format to write down the month in letters and the full year.
Example:
Januar 2024
Februar 2024
...
Decembar 2024
etc.
This worked fine for all months except the edge case for December where the output resulted in:
Decembar 2025
Example:
$period = new DateTimeImmutable('2024-12-31');
$format = new IntlDateFormatter('sr-Latn', IntlDateFormatter::NONE, IntlDateFormatter::NONE, NULL, IntlDateFormatter::GREGORIAN, "MMMM Y");
$label = datefmt_format($format, $period);
echo $label;
The output is: decembar 2025
the month is correct but the year changed
The result is the same even if the date is '2024-12-30'
, only when the date is lower then "2024-12-29"
will the correct result show up "decembar 2024"
.
$period = new DateTimeImmutable('2024-12-30');
$format = new IntlDateFormatter('sr-Latn', IntlDateFormatter::NONE, IntlDateFormatter::NONE, NULL, IntlDateFormatter::GREGORIAN, "MMMM Y");
$label = datefmt_format($format, $period);
echo $label;
So my question is can anyone explain this weird interaction, i expected it has to do with Time zones, but that would explain why both 30 and 31st December would result the same. Also why does the year change but not he month, if it was a timezone but i would expect it to be January 2025, not December?
Tested on php7.4 and php8.2
Default timezone is: "Europe/Berlin"
At the end of each month i want to write down the month in serbian latin alphabeth format to write down the month in letters and the full year.
Example:
Januar 2024
Februar 2024
...
Decembar 2024
etc.
This worked fine for all months except the edge case for December where the output resulted in:
Decembar 2025
Example:
$period = new DateTimeImmutable('2024-12-31');
$format = new IntlDateFormatter('sr-Latn', IntlDateFormatter::NONE, IntlDateFormatter::NONE, NULL, IntlDateFormatter::GREGORIAN, "MMMM Y");
$label = datefmt_format($format, $period);
echo $label;
The output is: decembar 2025
the month is correct but the year changed
The result is the same even if the date is '2024-12-30'
, only when the date is lower then "2024-12-29"
will the correct result show up "decembar 2024"
.
$period = new DateTimeImmutable('2024-12-30');
$format = new IntlDateFormatter('sr-Latn', IntlDateFormatter::NONE, IntlDateFormatter::NONE, NULL, IntlDateFormatter::GREGORIAN, "MMMM Y");
$label = datefmt_format($format, $period);
echo $label;
So my question is can anyone explain this weird interaction, i expected it has to do with Time zones, but that would explain why both 30 and 31st December would result the same. Also why does the year change but not he month, if it was a timezone but i would expect it to be January 2025, not December?
Tested on php7.4 and php8.2
Default timezone is: "Europe/Berlin"
Share Improve this question edited 22 hours ago Your Common Sense 158k42 gold badges221 silver badges363 bronze badges asked 23 hours ago Igor VidicIgor Vidic 213 bronze badges New contributor Igor Vidic is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 02 Answers
Reset to default 4Look up what the Y
symbol means:
Symbol | Meaning |
---|---|
y |
year |
Y |
year in “Week of Year” based calendars in which the year transition occurs on a week boundary; may differ from calendar year ‘y’ near a year transition. This year designation is used with pattern character ‘w’ in the ISO 8601 year-week calendar, for example. |
Source: https://unicode-org.github.io/icu/userguide/format_parse/datetime/, linked to from PHP's documentation.
In other words, you want y
, not Y
.
I believe it has todo with the formatter of the year, you should be able to use O instead of Y
$period = new DateTimeImmutable('2024-12-31');
$format = new IntlDateFormatter('sr-Latn', IntlDateFormatter::NONE, IntlDateFormatter::NONE, NULL, IntlDateFormatter::GREGORIAN, "MMMM o");
$label = datefmt_format($format, $period);
echo $label;
This is what php writes about using o instead of y
"ISO 8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead."
https://www.php.net/manual/en/datetime.format.php
本文标签:
版权声明:本文标题:php - Why does uppercase Y shows the next year for the dates close to the year end when using IntlDateFormatter? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736583372a1944977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论