我正试图依靠IntlDateFormatter来,以基于区域设置的格式返回当前日期。这意味着DD/MM/YYYY对it_IT,de_DE,fr_FR.或MM/DD/YYYY表示en_US,等等,用于所有可能的地区。
我试图遵循与检索当前月份名称相同的解决方案:
$formatter = new IntlDateFormatter("it_IT", IntlDateFormatter::FULL, IntlDateFormatter::FULL);
$formatter->setPattern("MMMM");
return $formatter->format($this);此代码正确地返回“一月”、"gennaio“等。
问题是,我无法找到正确的模式来获得当前地区格式的日期。ICU用户指南提到了DateTimePatternGenerator类,这看起来很像!但我在PHP中找不到它。
我想避免一个自定义的,巨大的开关箱和依赖内置功能代替。
它必须是纯PHP.
有什么帮助吗?
发布于 2015-02-28 13:50:43
我不确定您是想从ICU本身获取模式,还是只是格式化您的DateTime。这两种解决方案都有。
Basics:
您在检索月份名称的实际代码方面做得很好。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);会给你
28/02/2015
您可以使用IntlDateFormatter的其他选项。有短、中、长、满和无--您可以阅读它们并查看示例ouput 这里。
获取模式 来自格式化程序
$formatter->->getPattern();会给你这样的东西
dd/MM/yyyy
我希望这能帮到你。很长一段时间以来,我一直在努力做好这件事:)
https://stackoverflow.com/questions/28172415
复制相似问题