views:

28

answers:

1

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.

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"?)

A: 

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();
Stefan Gehrig
I know that I can manually set the timezone on every Zend_Date object, but I was wondering if there is a smarter way to display the time in the right timezone in my views.I don't want to do a $this->model->dateCreated->setTimezone(date_default_timezone_get())->format(Zend_Date::DATE_MEDIUM); every time I want to output a date.
Sebastian Hoitz
OK - I understand. As far as I know there is no built-in view helper for this task, so writing your own should be the way to go.
Stefan Gehrig