为IntlDateFormatter()生成适当的模式->;PHP中基于区域设置的format()

为IntlDateFormatter()生成适当的模式->;PHP中基于区域设置的format(),php,datetime,internationalization,Php,Datetime,Internationalization,我正试图依靠IntlDateFormatter以基于区域设置的格式返回当前日期。这意味着它的DD/MM/YYYY\u it,de\u de,fr\u fr。。。或MM/DD/YYYY(适用于美国)等所有可能的地区 我尝试使用与检索当前月份名称相同的解决方案: $formatter = new IntlDateFormatter("it_IT", IntlDateFormatter::FULL, IntlDateFormatter::FULL); $formatter->setPattern

我正试图依靠IntlDateFormatter以基于区域设置的格式返回当前日期。这意味着它的DD/MM/YYYY\u it,de\u de,fr\u fr。。。或MM/DD/YYYY(适用于美国)等所有可能的地区

我尝试使用与检索当前月份名称相同的解决方案:

$formatter = new IntlDateFormatter("it_IT", IntlDateFormatter::FULL, IntlDateFormatter::FULL);
$formatter->setPattern("MMMM");
return $formatter->format($this);
此代码正确返回“一月”、“gennaio”等

问题是,我无法找到正确的模式以当前区域设置格式获取日期。中提到了日期时间模式生成器类,这看起来很像!但是我在PHP中找不到它

我想避免一个定制的,巨大的开关盒,而是依靠内置的功能

它必须是纯PHP。


有什么帮助吗?

我不确定您是想从ICU本身获取模式,还是只是格式化日期时间。这里有两种解决方案

基础知识:

$locale = "en_GB";
$dateType = IntlDateFormatter::SHORT;//type of date formatting
$timeType = IntlDateFormatter::NONE;//type of time formatting setting to none, will give you date itself
$formatter =new IntlDateFormatter($locale, $dateType, $timeType);
$dateTime = new DateTime("2015-02-28");
echo $formatter->format($dateTime);
您在检索月份名称的实际代码方面做得很好。IntlDateFormatter是非常可定制的,它完全取决于您提供的选项

根据地区获取年、月、日的日期:

$locale = "en_GB";
$dateType = IntlDateFormatter::SHORT;//type of date formatting
$timeType = IntlDateFormatter::NONE;//type of time formatting setting to none, will give you date itself
$formatter =new IntlDateFormatter($locale, $dateType, $timeType);
$dateTime = new DateTime("2015-02-28");
echo $formatter->format($dateTime);
我会给你

2015年2月28日

您可以使用IntlDateFormatter的其他选项。有短的、中的、长的、完整的和无的-您可以阅读它们并检查示例输出

来自格式化程序

$formatter->->getPattern();
会给你类似的东西

年月日


我希望这对你有帮助。我一直在努力解决这个问题很久了:)

你愿意使用一些JS代码吗?我添加了“它必须是纯PHP”,谢谢。我有一个非常类似的问题-并且发现目前只有HHVM支持它。我们所能做的就是等待。看看为什么现在不可能。