开发者

Showing timezone specific dates in views with Zend_Date

开发者 https://www.devze.com 2023-01-11 20:03 出处:网络
I\'m storing all my dates in ISO-format, so all of them look like this: 2010-08-17T12:47:59+00:00 Now, when my application starts, I register the timezone the current user resides in. In my case, t

I'm storing all my dates in ISO-format, so all of them look like this:

2010-08-17T12:47:59+00:00

Now, when my application starts, I register the timezone the current user resides in. In my case, this would be "Europe/Berlin":

date_default_timezone_set("Europe/Berlin");

However, when Zend_Date parses ISO dates, it overrides the default timezone set earlier and now has the UTC timezone.

开发者_StackOverflow中文版But when I output this date in my view scripts I want it to show the date in the correct timezone.

Are there better solutions than writing a custom view helper just for this? (If this was the correct solution, shouldn't there already be a "DateViewHelper"?)


Not sure how this works with Zend_Date but given the fact that you use PHP >= 5.2 you can use the built-in DateTime class (which offers less functionality but is extremely faster):

$date = new DateTime('2010-08-17T12:47:59+00:00');
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo $date->format(DateTime::W3C);

EDIT

Just checked Zend_Date and it actually works the same here...

$date = new Zend_Date('2010-08-17T12:47:59+00:00', Zend_Date::ISO_8601);
$date->setTimezone(date_default_timezone_get());
echo $date->getIso();
0

精彩评论

暂无评论...
验证码 换一张
取 消