douyin2883 2016-02-01 11:55 采纳率: 0%
浏览 50
已采纳

如何使用MEDIUM或FULL日期类型使用IntlDateFormatter格式化日期,但没有一年?

First of all: I know that I can manually create a bunch of config files with corresponding patterns for every locale; actually I try to find a workaround with using only IntlDateFormatter.

I'll try to explain with examples.

<?php

$tz = new DateTimeZone('Europe/Moscow');
$now = time();
foreach (['en_US', 'ja_JA', 'ru_RU'] as $locale) {
    printf("%s:
", $locale);

    foreach ([IntlDateFormatter::MEDIUM, IntlDateFormatter::LONG] as $datetype) {
        $formatter = new IntlDateFormatter($locale, $datetype, IntlDateFormatter::NONE);
        printf("- %s
", $formatter->format($now));
    }
}

https://3v4l.org/iJOT4

This produces

en_US:
- Feb 1, 2016
- February 1, 2016
ja_JA:
- 2016/02/01
- 2016年2月1日
ru_RU:
- 1 февр. 2016 г.
- 1 февраля 2016 г.

And I need

en_US:
- Feb 1
- February 1
ja_JA:
- 02/01
- 2月1日
ru_RU:
- 1 февр.
- 1 февраля

The first idea was to extract a pattern for given locale and remove any 'y' and 'Y' letters. But as you can see, there is more than just a 4-digit year: all that commas, slashes, labels (like 'г.' and '年').

PS:

Actually, what I want from an ideal IntlDateFormatter implementation is a smart pattern, where all components are in their places, but I can configure what pattern to use for each component. Like: instead of 'd MMMM y г.' pattern for ru_RU formatter has a 'dmy' pattern, and for d component it has d pattern, m - MMMM and y - y г.. So I could say y component is a '' (empty string), and voila.

Also, if you are aware of any library that already does this — please, let me know.

  • 写回答

1条回答 默认 最新

  • doushou1298 2016-02-02 09:45
    关注

    So, currently it's impossible. I've found that intl PHP extension simply lacks of DateTimePatternGenerator (http://userguide.icu-project.org/formatparse/datetime), which is exactly what I need.

    The DateTimePatternGenerator class provides a way to map a request for a set of date/time fields, along with their width, to a locale-appropriate format pattern. The request is in the form of a “skeleton” which just contains pattern letters for the desired fields using the representation for the desired width. In a skeleton, anything other than a pattern letter is ignored, field order is insignificant, and there are two special additional pattern letters that may be used: 'j' requests the preferred hour-cycle type for the locale (it gets mapped to one of 'H', 'h', 'k', or 'K'); 'J' is similar but requests no AM/PM marker even if the locale’s preferred hour-cycle type is 'h' or 'K'.

    For example, a skeleton of “MMMMdjmm” might result in the following format patterns for different locales:

    locale | format pattern for skeleton “MMMMdjmm” | example
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    en_US    "MMMM d  'at'  h:mm a"                   April 2 at 5:00 PM
    es_ES    "d 'de' MMMM, H:mm"                      2 de abril, 17:00
    ja_JP    "M月d日 H:mm"                              4月2日 17:00
    

    Also, I've found that HHVM has implemented it — https://github.com/facebook/hhvm/commit/bc84daf7816e4cd268da59d535dcadfc6cf01085 . I hope one day this will be ported to PHP.

    UPD: I've written a huge post on the problem — https://blog.ksimka.com/a-long-journey-to-formatting-a-date-without-a-year-internationally-with-php/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元