views:

281

answers:

3

I'm working with Zend 1.8. I've set the default timezone to Europe/Helsinki, and I'm parsing a string that looks like this:

2009-08-06

with a statement like this:

 new Zend_Date($dateStr, 'YYYY-MM-dd');

It produces a date like this:

object(Zend_Date)#53 (8) {
  ["_locale:private"]=>
  string(5) "en_US"
  ["_fractional:private"]=>
  int(0)
  ["_precision:private"]=>
  int(3)
  ["_unixTimestamp:private"]=>
  string(10) "1249502400"
  ["_timezone:private"]=>
  string(15) "Europe/Helsinki"
  ["_offset:private"]=>
  int(-7200)
  ["_syncronised:private"]=>
  int(0)
  ["_dst:protected"]=>
  bool(true)
}

So it apparently knows the time zone. However, when I try to get a string representation of the date, what I get isn't 2009-08-06, but instead 2009-08-05 11:00:00 PM -- the UTC time. What gives?

Edit: I added an answer as well, but the cliff notes versio is, Zend_Date::getDate() is broken, not the parsing or printing bits.

+1  A: 

Hi,

Did you trying setting a locale in the Registry ? Something like that :

$locale = new Zend_Locale('fr_FR');
Zend_Registry::set('Zend_Locale', $locale);

(Adapted to your locale, of course)

Would it help ?

Pascal MARTIN
Yes, indeed, it would!
Rytmis
No wait, false alarm, sorry! (Bad testcase)
Rytmis
Ergh, too bad :-( sorry about that, then :-(
Pascal MARTIN
A: 

Look at the Zend_Date __construct DocBlock:

  • Always set the default timezone: http://php.net/date_default_timezone_set
  • For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
  • For detailed instructions please look in the docu.

If so maybe you need to add the locale to the call eg.

Zend_Date($dateStr,'Format','de_DE');

Rufinus
If you look at my question again, you'll notice that the second sentence begins with "I've set the default timezone to Europe/Helsinki". And the locale has zero effect on the timezone offset, all it does is controls the input and output format. :(
Rytmis
+1  A: 

Well, as per usual, my assumptions were faulty. I went back to check all the steps that might go wrong, and as it happens, the timezones work fine when parsing and printing. The trouble is Zend_Date::getDate(). The documentation says the following:

Returns a clone of $this, with the time part set to 00:00:00.

However, when I actually use it:

$date = Zend_Date::now();
$date = $date->getDate();

The result is

Aug 8, 2009 11:00:00 PM

Now, that's decidedly not 00:00:00.

This looks like Zend Bug 4490, although it's supposedly resolved in 1.7.0 and I'm running 1.8.1. I guess I'll have to reopen the bug.

Rytmis