使用 Zend 语言环境进行时间转换

发布于 2024-11-24 07:58:35 字数 941 浏览 3 评论 0原文

有没有办法使用 Zend Locale 转换时间(例如 17:00 到 5:00pm)?

我已经尝试了文档中的方法(有错字),但它不起作用。它给出错误“无法使用 'dd.MM.yyyy' (M <> y) 解析日期 '13:44:42'”

$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::getTime('13:44:42',
                            array('date_format' =>
                                      Zend_Locale_Format::STANDARD,
                                  'locale' => $locale))) {
    print "time";
} else {
    print "not a time";
}

然后我尝试了两步方法,首先获取当前区域设置的时间格式,然后在 getTime 函数中使用它。

$locale = new Zend_Locale('en_US');
$tf = Zend_Locale_Format::getTimeFormat($locale);
$test = Zend_Locale_Format::getTime('17:00', array('date_format' => $tf, 'locale' => $locale));

这会返回一个结果,但只是返回我所拥有的内容

array('date_format'=>'h:mm:ss a', 'locale'=>'en_US', 'hour'=>'17', 'minute'=>'00')

是否有什么东西可以将时间转换为我试图解析它的实际语言环境?

Is there a way of converting a time, say 17:00 to 5:00pm using Zend Locale?

I've tried the method in the docs as it is (which has a typo), but it doesn't work. It gives the error 'Unable to parse date '13:44:42' using 'dd.MM.yyyy' (M <> y)'

$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::getTime('13:44:42',
                            array('date_format' =>
                                      Zend_Locale_Format::STANDARD,
                                  'locale' => $locale))) {
    print "time";
} else {
    print "not a time";
}

I then tried a 2 step method, getting the time format of the current locale first, and then using that in the getTime function.

$locale = new Zend_Locale('en_US');
$tf = Zend_Locale_Format::getTimeFormat($locale);
$test = Zend_Locale_Format::getTime('17:00', array('date_format' => $tf, 'locale' => $locale));

This returns a result but just gives me back what I had

array('date_format'=>'h:mm:ss a', 'locale'=>'en_US', 'hour'=>'17', 'minute'=>'00')

Is there something that will convert the time to the actual locale I'm trying to parse it to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姜生凉生 2024-12-01 07:58:38

您需要将 Zend_Date 与语言环境结合使用,以获取所需格式的日期。

$date    = new Zend_Date(); // Default ISO format type & en_US
// Set the time and pass the format I am using to set the time
$date->setTime('17:00:00', 'HH:mm:ss'); 
echo $date; // Jul 15, 2011 5:00:00 PM

编辑

有关如何将 Zend_LocaleZend_Date 结合使用的更多信息

$locale = new Zend_Locale('de_AT');
echo  $date->toString(Zend_Locale_Format::getTimeFormat($locale)) ; // 17:00:00
$locale = new Zend_Locale('en_US');
echo  $date->toString(Zend_Locale_Format::getTimeFormat($locale)) ; // 5:00:00 PM

You need to be using Zend_Date with the locale to get the date in the format you want.

$date    = new Zend_Date(); // Default ISO format type & en_US
// Set the time and pass the format I am using to set the time
$date->setTime('17:00:00', 'HH:mm:ss'); 
echo $date; // Jul 15, 2011 5:00:00 PM

EDIT

More on how you can use Zend_Locale with Zend_Date

$locale = new Zend_Locale('de_AT');
echo  $date->toString(Zend_Locale_Format::getTimeFormat($locale)) ; // 17:00:00
$locale = new Zend_Locale('en_US');
echo  $date->toString(Zend_Locale_Format::getTimeFormat($locale)) ; // 5:00:00 PM
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文