格式化DateTime对象,尊重Locale :: getDefault()

sto*_*fln 57 php datetime-format

我有一个DateTime对象,我正在通过它编写

$mytime->format("D d.m.Y")
Run Code Online (Sandbox Code Playgroud)

这给了我完全符合我需要的格式:

星期二5.3.2012

唯一缺少的是正确的语言.我需要Tue(Tuesday)的德语翻译,即Die(Dienstag).

这为我提供了正确的区域设置

Locale::getDefault()
Run Code Online (Sandbox Code Playgroud)

但我不知道怎么告诉DateTime::format它使用它.

有没有办法做这样的事情:

$mytime->format("D d.m.Y", \Locale::getDefault());
Run Code Online (Sandbox Code Playgroud)

sal*_*the 113

您可以使用Intl扩展名格式化日期.它将根据所选的区域设置格式化日期/时间,或者您可以使用IntlDateFormatter::setPattern().

使用自定义模式的快速示例可能看起来像您想要的输出格式.

$dt = new DateTime;

$formatter = new IntlDateFormatter('de_DE', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
$formatter->setPattern('E d.M.yyyy');

echo $formatter->format($dt);
Run Code Online (Sandbox Code Playgroud)

其中输出以下内容(至少今天).

迪.2013年6月4日


编辑:啊嘘!回答了一个古老的问题,因为有些评论将其列入了清单!现在至少提到了Intl选项.

  • 您应该知道 PHP 的 IntlDateFormatter 类使用 ISO 日期格式代码而不是 PHP 的 date() 格式代码。I 查找 ISO 代码的好列表位于 http://framework.zend.com/manual/1.12/en/zend.date.constants.html#zend.date.constants.selfdefineformats (3认同)
  • 很高兴你回答了一个古老的问题,因为这个问题会出现在谷歌搜索结果的顶部,并且过去只提供过时的信息。 (2认同)
  • 您可以按如下方式轻松安装扩展:`yum install php-intl` `apachectl reload` (2认同)

Jon*_*han 58

那是因为format不注意语言环境.你应该使用strftime.

例如:

setlocale(LC_TIME, "de_DE"); //only necessary if the locale isn't already set
$formatted_time = strftime("%a %e.%l.%Y", $mytime->getTimestamp())
Run Code Online (Sandbox Code Playgroud)

  • strftime()不适用于1970年之前的日期,Date_time :: format()不适用于locales ...是否有适用于这两种情况的东西? (24认同)
  • 在[DateTime :: format](http://www.php.net/manual/en/datetime.format.php)上的php文档中注明了这种行为:*`此方法不使用语言环境.所有输出均为英文.* (10认同)
  • 实际上,我们发现strftime()确实可以使用1970年以前的日期,如果你传递一个(负)unix时间戳(至少在我们的服务器上)...所以我们做这两者的组合:`$ datetime = new日期时间( '1920年1月1日'); setLocale(LC_TIME | LC_CTYPE,$ locale_based_on_user_profile); return strftime($ format,$ datetime-> format('U'));` (4认同)
  • strftime() 函数在 PHP 8.1 中已弃用,并将在 PHP 9 中删除。请参阅有关带​​有 Intl 扩展的 DateTIme 的得票最高的答案。 (4认同)

MrS*_*les 15

IntlDateFormatter是当前(2023 年)的发展方向。

\n
<?php\n$formatter = new IntlDateFormatter(\n    $locale,  // the locale to use, e.g. \'en_GB\'\n    $dateFormat,  // how the date should be formatted, e.g. IntlDateFormatter::FULL\n    $timeFormat,  // how the time should be formatted, e.g. IntlDateFormatter::FULL \n    \'Europe/Berlin\'  // the time should be returned in which timezone?\n);\n\necho $formatter->format(time());\n
Run Code Online (Sandbox Code Playgroud)\n

将给出不同的输出,具体取决于您传递的内容$locale以及日期和时间格式。我想添加一些示例以供将来参考。请注意,IntlDateFormatter::GREGORIANIntlDateFormatter::LONG是可以互换的。

\n
Locale: en_US\nFormat for Date & Time:           Results in:\nIntlDateFormatter::FULL           Friday, August 5, 2022 at 3:26:37 PM Central European Summer Time \nIntlDateFormatter::LONG           August 5, 2022 at 3:26:37 PM GMT+2 \nIntlDateFormatter::MEDIUM         Aug 5, 2022, 3:26:37 PM \nIntlDateFormatter::SHORT          8/5/22, 3:26 PM \n\n\n\nLocale: en_GB\nFormat for Date & Time:           Results in:\nIntlDateFormatter::FULL           Friday, 5 August 2022 at 15:26:37 Central European Summer Time \nIntlDateFormatter::LONG           5 August 2022 at 15:26:37 CEST \nIntlDateFormatter::MEDIUM         5 Aug 2022, 15:26:37 \nIntlDateFormatter::SHORT          05/08/2022, 15:26 \n\n\n\nLocale: de_DE\nFormat for Date & Time:           Results in:\nIntlDateFormatter::FULL           Freitag, 5. August 2022 um 15:26:37 Mitteleurop\xc3\xa4ische Sommerzeit \nIntlDateFormatter::LONG           5. August 2022 um 15:26:37 MESZ \nIntlDateFormatter::MEDIUM         05.08.2022, 15:26:37 \nIntlDateFormatter::SHORT          05.08.22, 15:26 \n\n\n\nLocale: fr_FR\nFormat for Date & Time:           Results in:\nIntlDateFormatter::FULL           vendredi 5 ao\xc3\xbbt 2022 \xc3\xa0 15:26:37 heure d\xe2\x80\x99\xc3\xa9t\xc3\xa9 d\xe2\x80\x99Europe centrale \nIntlDateFormatter::LONG           5 ao\xc3\xbbt 2022 \xc3\xa0 15:26:37 UTC+2 \nIntlDateFormatter::MEDIUM         5 ao\xc3\xbbt 2022 \xc3\xa0 15:26:37 \nIntlDateFormatter::SHORT          05/08/2022 15:26 \n\n
Run Code Online (Sandbox Code Playgroud)\n

正如 salathe 已经说过的,$formatter->setPattern如果需要,您还可以用来进一步自定义输出。

\n