使用Zend_Date处理日期和时区

时间:2022-10-28 20:45:18

I have an application which uses Zend_Date to display dates. Zend_Date instances are created using datetime data from MySQL, user input and the current date.

我有一个使用Zend_Date显示日期的应用程序。使用来自MySQL的日期时间数据,用户输入和当前日期创建Zend_Date实例。

I would like for my users to be able to specify their timezone and for all dates to be displayed in their local time.

我希望我的用户能够指定他们的时区,并且所有日期都以当地时间显示。

At the moment my code works like this:

目前我的代码是这样的:

$date = '2009-01-01 10:30:00';

$date = new Zend_Date($date, Zend_Date::ISO_8601);

echo $date->get(Zend_Date::TIME_MEDIUM); //10:30:00

$date->setTimezone('Australia/ACT');

echo $date->get(Zend_Date::TIME_MEDIUM); //21:30:00

This works, but requires a setTimezone call on every date. Is there an easier way to manage timezones?

这有效,但每个日期都需要调用setTimezone。有没有更简单的方法来管理时区?

I've also been looking at using SET time_zone with MySQL, to return adjusted data from MySQL. Then I would only need to adjust dates created within PHP scripts for timezones.

我一直在寻找使用SET time_zone与MySQL,从MySQL返回调整后的数据。然后我只需要调整PHP脚本中为时区创建的日期。

I'd love to hear the best way to handle this, if someone has experience.

如果有人有经验,我很乐意听到解决这个问题的最佳方法。

Thanks

2 个解决方案

#1


I think that setting the PHP timezone should set the default for all subsequent Zend_Date instances. For example:

我认为设置PHP时区应该为所有后续Zend_Date实例设置默认值。例如:

date_default_timezone_set('Europe/Vienna');

From the Zend_Date section in the Zend Framework Reference Guide:

从Zend Framework Reference Guide中的Zend_Date部分:

In PHP, we can adjust all date and time related functions to work for a particular user by setting a default timezone according to the user's expectations. When creating Zend_Date instances, their timezone will automatically become the current default timezone!

在PHP中,我们可以根据用户的期望设置默认时区,调整所有日期和时间相关功能,以便为特定用户工作。创建Zend_Date实例时,它们的时区将自动成为当前的默认时区!

#2


I think you could make use of the Zend_Locale, read some docs about it, pretty sure you'd be able to make it work. On the other hand, if you use Zend_Cache and Zend_Locale/Zend_Date, that's gonna help up improve the speed by quite a bit. There are examples of usage in the zend framework docs too.

我想你可以使用Zend_Locale,阅读一些关于它的文档,非常确定你能够使它工作。另一方面,如果你使用Zend_Cache和Zend_Locale / Zend_Date,这将有助于提高速度。 zend框架文档中也有使用示例。

#1


I think that setting the PHP timezone should set the default for all subsequent Zend_Date instances. For example:

我认为设置PHP时区应该为所有后续Zend_Date实例设置默认值。例如:

date_default_timezone_set('Europe/Vienna');

From the Zend_Date section in the Zend Framework Reference Guide:

从Zend Framework Reference Guide中的Zend_Date部分:

In PHP, we can adjust all date and time related functions to work for a particular user by setting a default timezone according to the user's expectations. When creating Zend_Date instances, their timezone will automatically become the current default timezone!

在PHP中,我们可以根据用户的期望设置默认时区,调整所有日期和时间相关功能,以便为特定用户工作。创建Zend_Date实例时,它们的时区将自动成为当前的默认时区!

#2


I think you could make use of the Zend_Locale, read some docs about it, pretty sure you'd be able to make it work. On the other hand, if you use Zend_Cache and Zend_Locale/Zend_Date, that's gonna help up improve the speed by quite a bit. There are examples of usage in the zend framework docs too.

我想你可以使用Zend_Locale,阅读一些关于它的文档,非常确定你能够使它工作。另一方面,如果你使用Zend_Cache和Zend_Locale / Zend_Date,这将有助于提高速度。 zend框架文档中也有使用示例。