日期格式在 PHP 中使用 DateFmt 在法语中返回错误的年份
我写了这个函数,
function formatted_month($month) {
$first_day_in_month = new DateTime('2022-01-01');
$fmt = datefmt_create(
'en_EN',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Paris',
IntlDateFormatter::GREGORIAN,
'MMMM YY'
);
$formatted_month = datefmt_format($fmt,$first_day_in_month);
return $formatted_month;
}
按预期返回 1 月 22 日,但是这段代码:
function formatted_month($month) {
$first_day_in_month = new DateTime('2022-01-01');
$fmt = datefmt_create(
'fr_FR',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Paris',
IntlDateFormatter::GREGORIAN,
'MMMM YY'
);
$formatted_month = datefmt_format($fmt,$first_day_in_month);
return $formatted_month;
}
返回 janvier 21。唯一的区别是这一行
'en_EN', -> 'fr_FR'
对于 es_ES 和非英语语言来说是一样的......
你有什么解释吗? 谢谢
I wrote this function
function formatted_month($month) {
$first_day_in_month = new DateTime('2022-01-01');
$fmt = datefmt_create(
'en_EN',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Paris',
IntlDateFormatter::GREGORIAN,
'MMMM YY'
);
$formatted_month = datefmt_format($fmt,$first_day_in_month);
return $formatted_month;
}
This return January 22 as expected but this code :
function formatted_month($month) {
$first_day_in_month = new DateTime('2022-01-01');
$fmt = datefmt_create(
'fr_FR',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Paris',
IntlDateFormatter::GREGORIAN,
'MMMM YY'
);
$formatted_month = datefmt_format($fmt,$first_day_in_month);
return $formatted_month;
}
Return janvier 21. The only difference is this line
'en_EN', -> 'fr_FR'
Same thing for es_ES and non English languages...
Do you have any explanation ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是格式化字符串的一个微妙错误,即存在多个不同的年份说明符:
yy
或yyyy
是您期望的正常年份,与月份和日期一起使用YY
或YYYY
是与“一年中的一周”一起使用的年份“对于大多数日期来说,这些是相同,所以差异很容易被忽视。但在一年的年初和年末,一周的开始和结束位置很重要——1 月 1 日可能仍然是上一年的最后一周,或者 12 月 31 日可能是下一年的第一周。
如果我们使用
'ww YYYY, MMMM yyyy'
格式字符串来显示周数和年份的两种定义,我们可以清楚地看到差异:en_GB
我们得到“01 2022, January 2022”fr_FR
我们得到“52 2021, janvier 2022”因此始终使用小写“yy”形式,除非您实际显示周数。
This is a subtle mistake of the formatting string, which is that there are multiple different year specifiers:
yy
oryyyy
is the normal year you would expect, for use with month and dayYY
orYYYY
is the year for use with "week of year"For the majority of dates, these are the same, so the difference is easy to overlook. But at the very beginning and end of the year, it matters where the week begins and ends - January 1st may still be in the last week of the previous year, or December 31st may be in the first week of the next year.
We can see the difference clearly if we use a format string of
'ww YYYY, MMMM yyyy'
, to show the week number and both definitions of year:en_GB
we get "01 2022, January 2022"fr_FR
we get "52 2021, janvier 2022"So always use the lower-case "yy" form, unless you are actually displaying week numbers.