首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IntlDateFormatter返回不正确的年份

IntlDateFormatter返回不正确的年份
EN

Stack Overflow用户
提问于 2020-01-10 13:39:24
回答 1查看 187关注 0票数 2

我有一个日期转换的问题,经过很多测试后,我不明白我错过了什么。

我从存储在MySQL数据库中的典型日期时间开始(2019-12-31 09:35:16)。

更准确地说,我在Windows上使用PHP7.2.10

下面是一个简单的例子:

代码语言:javascript
复制
$formatter = new IntlDateFormatter(
    'fr-FR',
    IntlDateFormatter::NONE,
    IntlDateFormatter::NONE,
    new DateTimeZone('Europe/Paris'),
    IntlDateFormatter::GREGORIAN,
    'd/M/Y HH:mm \'UTC\'XXX'
);
echo $formatter->format(new \DateTime('2019-12-31 09:35:16'));

答案给了我31/12/2020 09:35 UTC+01:00而不是31/12/2019 09:35 UTC+01:00

经过一些尝试,2013年和2025年也出现了这一变化(很可能每六年发生一次.)。

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-10 14:02:31

这是一种不同的格式。它不使用PHP日期格式,它使用ICU数据格式。y/Y格式不同:

代码语言:javascript
复制
Date Field Symbol Table

Symbol  Meaning                   Example(s)
y       year                      (yy or y or yyyy) 96 or 1996
Y       year of "Week of Year"    Y   1997

由于2019-12-31是一个星期二,它属于2020年的周年.相反,您将使用y作为格式。

代码语言:javascript
复制
$formatter = new IntlDateFormatter(
    'fr-FR',
    IntlDateFormatter::NONE,
    IntlDateFormatter::NONE,
    new DateTimeZone('Europe/Paris'),
    IntlDateFormatter::GREGORIAN,
    'd/M/y HH:mm \'UTC\'XXX'
);
echo $formatter->format(new \DateTime('2019-12-31 09:35:16'));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59682843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档