格式化DateTime对象,遵守Locale :: getDefault() [英] Formatting DateTime object, respecting Locale::getDefault()
本文介绍了格式化DateTime对象,遵守Locale :: getDefault()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DateTime对象,当前通过该对象进行格式化
I have a DateTime object which I'm currently formating via
$mytime->format("D d.m.Y")
这给了我确切的格式:
2012年2月5.3日
Tue 5.3.2012
唯一缺少的一点是正确的语言.我需要Tue
(Tuesday
)的德语翻译,这是Die
(Dienstag
).
The only missing point is the correct language. I need German translation of Tue
(Tuesday
), which is Die
(Dienstag
).
这给了我正确的语言环境设置
This gives me the right locale setting
Locale::getDefault()
但是我不知道如何告诉DateTime::format
使用它.
But I don't know how to tell DateTime::format
to use it.
没有办法做类似的事情:
Isn't there a way to do something like:
$mytime->format("D d.m.Y", \Locale::getDefault());
推荐答案
这是因为format
不注意语言环境.您应该改用 strftime
.
That's because format
does not pay attention to locale. You should use strftime
instead.
例如:
setlocale(LC_TIME, "de_DE"); //only necessary if the locale isn't already set
$formatted_time = strftime("%a %e.%l.%Y", $mytime->getTimestamp())
这篇关于格式化DateTime对象,遵守Locale :: getDefault()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文