Localized (short) month names using IntlDateFormatter in PHP?(在 PHP 中使用 IntlDateFormatter 本地化(短)月份名称?)
问题描述
在我的 Windows 开发机器上,我已将语言环境设置为 ita
:
On my Windows development machine, I have set the locale to ita
:
setlocale(LC_TIME, 'ita');
echo strftime('%b'); // dic
虽然(我想,现在无法测试)在 *nix 系统上,但我应该使用 it
:
While (I suppose, can't test right now) on a *nix system, I should use it
:
setlocale(LC_TIME, 'it');
如果我尝试在我的 Windows 上设置 it
它不起作用,打印 Dec
.
If I try to set it
on my Windows it doesn't work, printing Dec
.
看来我不能依赖 setlocale()
,所以我应该按照@hakre 的建议使用 IntlDateFormatter
.但是,我可以找到给我月份名称和简短名称的常量:
It seems I can't rely on setlocale()
, so I should use IntlDateFormatter
as suggested by @hakre. However, I can find the constant that gives me the month name and the short one:
IntlDateFormatter::NONE (integer)
Do not include this element
IntlDateFormatter::FULL (integer)
Completely specified style (Tuesday, April 12, 1952 AD or 3:30:42pm PST)
IntlDateFormatter::LONG (integer)
Long style (January 12, 1952 or 3:30:32pm)
IntlDateFormatter::MEDIUM (integer)
Medium style (Jan 12, 1952)
IntlDateFormatter::SHORT (integer)
Most abbreviated style, only essential data (12/13/52 or 3:30pm)
推荐答案
来自 参考,一些月份格式代码:
From the reference, some month formatting codes:
Symbol Meaning Example:
M month in year M or MM 09
MMM Sept
MMMM September
MMMMM S
设置格式的PHP函数是:IntlDateFormatter::setPattern
,一些例子:
The PHP function to set the format is: IntlDateFormatter::setPattern
, some examples:
class LocaleDateFormat
{
private $locale;
private $pattern;
public function __construct($pattern, $locale = 'en_US') {
$this->setLocale($locale);
$this->setPattern($pattern);
}
public function setLocale($locale) {
$this->locale = $locale;
}
public function setPattern($pattern) {
$this->pattern = $pattern;
}
public function localeFormat($locale, $date) {
$this->setLocale($locale);
return $this->format($date);
}
public function format($date) {
$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::FULL, IntlDateFormatter::FULL);
$formatter->setPattern($this->pattern);
return $formatter->format($date);
}
}
$dateFormat = new LocaleDateFormat('MMMM'); # Long Month Names
$date = new DateTime(); # Now
$locales = ["en_US", "de_DE", "sv_SE", "ru_RU"];
foreach ($locales as $i => $locale) {
$month = $dateFormat->localeFormat($locale, $date);
printf("%d. %s - %s
", $i+1, $locale, $month);
}
输出:
1. en_US - December
2. de_DE - Dezember
3. sv_SE - december
4. ru_RU - декабря
有关地区列表,请参见此处:
For a list of locales see here:
- PHP 中可用的整理器列表?
不同地区的短月份名称的实际示例:
The actual example for the short monthnames across different locales:
$locales = ["en_US", "de_DE", "sv_SE", "ru_RU", 'it', 'it_IT', 'it_CH'];
$dateFormat = new LocaleDateFormat('MMM'); # Short Month Names
$date = new DateTime(); # Now
foreach (range(1, 12) as $monthNumber)
{
$date->setDate(2012, $monthNumber, 1);
printf("%02d: ", $monthNumber);
foreach ($locales as $locale)
{
$monthLabel = $dateFormat->localeFormat($locale, $date);
$pad = str_repeat(' ', max(0, 8 - mb_strlen($monthLabel, 'UTF-8')));
printf("%s: %s%s ", $locale, $monthLabel, $pad);
}
echo "
";
}
示例输出:
01: en_US: Jan de_DE: Jan sv_SE: jan ru_RU: янв. it: gen it_IT: gen it_CH: gen
02: en_US: Feb de_DE: Feb sv_SE: feb ru_RU: февр. it: feb it_IT: feb it_CH: feb
03: en_US: Mar de_DE: Mär sv_SE: mar ru_RU: марта it: mar it_IT: mar it_CH: mar
04: en_US: Apr de_DE: Apr sv_SE: apr ru_RU: апр. it: apr it_IT: apr it_CH: apr
05: en_US: May de_DE: Mai sv_SE: maj ru_RU: мая it: mag it_IT: mag it_CH: mag
06: en_US: Jun de_DE: Jun sv_SE: jun ru_RU: июня it: giu it_IT: giu it_CH: giu
07: en_US: Jul de_DE: Jul sv_SE: jul ru_RU: июля it: lug it_IT: lug it_CH: lug
08: en_US: Aug de_DE: Aug sv_SE: aug ru_RU: авг. it: ago it_IT: ago it_CH: ago
09: en_US: Sep de_DE: Sep sv_SE: sep ru_RU: сент. it: set it_IT: set it_CH: set
10: en_US: Oct de_DE: Okt sv_SE: okt ru_RU: окт. it: ott it_IT: ott it_CH: ott
11: en_US: Nov de_DE: Nov sv_SE: nov ru_RU: нояб. it: nov it_IT: nov it_CH: nov
12: en_US: Dec de_DE: Dez sv_SE: dec ru_RU: дек. it: dic it_IT: dic it_CH: dic
这篇关于在 PHP 中使用 IntlDateFormatter 本地化(短)月份名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中使用 IntlDateFormatter 本地化(短)月份名称?
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01