如何使用IntlDateFormatter使用MEDIUM或FULL日期类型格式化日期,但没有年份? [英] How to format date with a IntlDateFormatter using MEDIUM or FULL datetype, but without a year?
问题描述
首先:我知道我可以为每个语言环境手动创建一堆具有相应模式的配置文件.实际上,我尝试通过仅使用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.
我将尝试举例说明.
<?php
$tz = new DateTimeZone('Europe/Moscow');
$now = time();
foreach (['en_US', 'ja_JA', 'ru_RU'] as $locale) {
printf("%s:\n", $locale);
foreach ([IntlDateFormatter::MEDIUM, IntlDateFormatter::LONG] as $datetype) {
$formatter = new IntlDateFormatter($locale, $datetype, IntlDateFormatter::NONE);
printf("- %s\n", $formatter->format($now));
}
}
这产生
en_US:
- Feb 1, 2016
- February 1, 2016
ja_JA:
- 2016/02/01
- 2016年2月1日
ru_RU:
- 1 февр. 2016 г.
- 1 февраля 2016 г.
我需要
en_US:
- Feb 1
- February 1
ja_JA:
- 02/01
- 2月1日
ru_RU:
- 1 февр.
- 1 февраля
第一个想法是提取给定语言环境的模式,并删除所有"y"和"Y"字母.但正如您所看到的,不仅仅只有4位数字的年份:所有逗号,斜杠和标签(例如г."和年").
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:
实际上,从理想的IntlDateFormatter
实现中我想要的是一个智能模式,其中所有组件都位于它们的位置,但是我可以配置要用于每个组件的模式.像:代替'd MMMM y г.'
模式,ru_RU
格式化程序具有'dmy'
模式,对于d
组件,它具有d
模式,m
-MMMM
和y
-y г.
.所以我可以说y
组件是''(空字符串),瞧.
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.
推荐答案
因此,目前尚不可能.我发现intl
PHP扩展名完全缺少DateTimePatternGenerator
( http://userguide .icu-project.org/formatparse/datetime ),这正是我所需要的.
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.
DateTimePatternGenerator类提供了一种将一组日期/时间字段及其宽度的请求映射到适合区域设置的格式模式的方法.该请求采用骨架"的形式,其中仅包含使用所需宽度的表示形式的所需字段的模式字母.在骨架中,除了模式字母之外的任何其他字符都将被忽略,字段顺序无关紧要,并且可以使用两个特殊的额外模式字母:'j'请求语言环境的首选小时周期类型(它映射到一个'H','h','k'或'K'); "J"类似,但是即使语言环境的首选小时周期类型为"h"或"K",也不需要AM/PM标记.
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'.
例如,"MMMMdjmm"的骨架可能会导致以下格式模式针对不同的语言环境:
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
此外,我发现HHVM已实现它- https://github.com /facebook/hhvm/commit/bc84daf7816e4cd268da59d535dcadfc6cf01085 .我希望有一天将其移植到PHP.
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::关于此问题,我写了一篇大文章- https://blog.ksimka.com/a-long-journey-to-formatting-a-date-without-a- year-internationally-with-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/
这篇关于如何使用IntlDateFormatter使用MEDIUM或FULL日期类型格式化日期,但没有年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!