日期格式在 PHP 中使用 DateFmt 在法语中返回错误的年份

发布于 2025-01-12 23:25:16 字数 996 浏览 0 评论 0原文

我写了这个函数,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2025-01-19 23:25:16

这是格式化字符串的一个微妙错误,即存在多个不同的年份说明符:

  • 小写 yyyyyy 是您期望的正常年份,与月份和日期一起使用
  • 大写 YYYYYY 是与“一年中的一周”一起使用的年份“

对于大多数日期来说,这些是相同,所以差异很容易被忽视。但在一年的年初和年末,一周的开始和结束位置很重要——1 月 1 日可能仍然是上一年的最后一周,或者 12 月 31 日可能是下一年的第一周。

如果我们使用 'ww YYYY, MMMM yyyy' 格式字符串来显示周数和年份的两种定义,我们可以清楚地看到差异:

  • 对于 en_GB 我们得到“01 2022, January 2022”
  • 对于 fr_FR 我们得到“52 2021, janvier 2022”

因此始终使用小写“yy”形式,除非您实际显示周数。

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;
}

This is a subtle mistake of the formatting string, which is that there are multiple different year specifiers:

  • A lower-case yy or yyyy is the normal year you would expect, for use with month and day
  • An upper-case YY or YYYY 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:

  • For en_GB we get "01 2022, January 2022"
  • For fr_FR we get "52 2021, janvier 2022"

So always use the lower-case "yy" form, unless you are actually displaying week numbers.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文