- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图依靠 IntlDateFormatter 来以基于语言环境的格式返回当前日期。这意味着 it_IT、de_DE、fr_FR 的 DD/MM/YYYY 或 en_US 的 MM/DD/YYYY 等等所有可能的语言环境。
我正在尝试使用与检索当前月份名称相同的解决方案:
$formatter = new IntlDateFormatter("it_IT", IntlDateFormatter::FULL, IntlDateFormatter::FULL);
$formatter->setPattern("MMMM");
return $formatter->format($this);
此代码正确返回“January”、“gennaio”等。
问题是,我无法找出正确的模式来获取当前语言环境格式的日期。 ICU User Guide提到了DateTimePatternGenerator 类,这看起来像!但我无法在 PHP 中找到它。
我想避免使用自定义的、巨大的 switch-case,而是依赖于内置函数。
必须是纯PHP。
有什么帮助吗?
最佳答案
我不确定你是想从 ICU 本身获取模式,还是只是格式化你的 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);
给你
28/02/2015
您可以使用 IntlDateFormatter 的其他选项。有 SHORT、MEDIUM、LONG、FULL 和 NONE - 您可以阅读它们并查看示例输出 here .
Getting pattern来自格式化程序
$formatter->->getPattern();
会给你类似的东西
dd/MM/yyyy
希望对您有所帮助。为了正确地做这件事,我已经苦苦挣扎了很长时间:)
关于php - 根据 PHP 中的语言环境为 IntlDateFormatter()->format() 生成适当的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28172415/
这段代码在 Java 中的等价物是什么?我放了一部分,我对 I/O 部分感兴趣: int fd = open(FILE_NAME, O_WRONLY); int ret = 0; if (fd =
我正在尝试将维度为 d1,d2,d3 的张量 M[a1,a2,a3] reshape 为维度为 d2, d1*d3 的矩阵 M[a2,a1*a3]。我试过 M.reshape(d2,d1*d3) 但是
我是一名优秀的程序员,十分优秀!