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选项.
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)
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());\nRun Code Online (Sandbox Code Playgroud)\n将给出不同的输出,具体取决于您传递的内容$locale以及日期和时间格式。我想添加一些示例以供将来参考。请注意,IntlDateFormatter::GREGORIAN和IntlDateFormatter::LONG是可以互换的。
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\nRun Code Online (Sandbox Code Playgroud)\n正如 salathe 已经说过的,$formatter->setPattern如果需要,您还可以用来进一步自定义输出。