Issues

ZF-11992: Wrong date in Zend_Date

Description

I have the code: $days = 12 $today = new Zend_Date(date('Y-m-d'), 'YYYY-MM-dd'); $minusTwoWeeks = $today->subDay($days); echo date('Y-m-d', $minusTwoWeeks->toValue());

Today is 2012-01-12 So I receive 2011-12-31 And it is correct. If I change $days to 10, I receive 2012-01-02 and it is also correct. But if I change $days to 11 I receive 2011-01-02. The year is incorrect! I tried several times, the result was the same. Then I waited 15 minutes, after that everything became good. 2012-01-01. But why it has changed?

Comments

Can you provide an exact date/time (down to the second) at which is error occurs? As you said it didn't recur when you tried again later, we'll need more than just the Ymd to dig deeper.

Just adding some example code that showing the problem http://pastebin.com/MFgbuy5f

This problem appears only when we use the 1st day of the year.

It happens in 1.11.11 and 1.9.6 (don't bother, it was a old Magento Install).

Fixed in trunk with r24880; will release with 1.12.0.

Basically, Zend_Date converts all formats to ISO 8601 standards. This means that "o" is used for the year instead of "Y". If you look at http://php.net/date you'll see an interesting note for "o" -- when the day of the week falls within the last week of the previous year, "o" will evaluate to the previous year. As such, I've added a regex that will replace "o" with "Y" when not preceded by an escape, thus giving us the desired behavior.

Uhm, however that was expected behaviour! See the note just before http://framework.zend.com/manual/1.12/…

I've been looking all over for this 'suddenly' altered behaviour. I'm sticking with the manual that states that using uppercase Y's will return me the week-based ISO year.

And, I'll add another comment to this one. I'd opened a new bug for this, however that got closed with the remark that the original one should be reopend. So, please do so. This implementation actually breaks my applications and it actually degrades my opinion of the stability of ZF since it doesn't even conform to its own manual anymore!

@Peter Can you help me and provide some examples which I can use for unit test? That would be great. I am very busy, because I work on the next release (1.12.2) and also on the ZF website.

Thanks in advance.

Sure, the test is quite simple (adhering to the manual that is :)). {quote} $date = new Zend_Date('2012-12-31'); echo $date->toString('c'); // ISO conforming string: 2012-12-31T00:00:00+01:00 (I'm in Europe/Amsterdam tz) $calendarYear = $date->get(Zend_Date::YEAR); // Expect 2012 $isoBasedYear = $date->get(Zend_Date::YEAR_8601); // Expect 2013 echo '
'.$calendarYear.' vs. '.$isoBasedYear; {quote} The original mentioned problem is the other way around (the first day of 2012 (2012-01-01) falls in the last week of the previous year and should render '2012 vs. 2011'). However it is simply a misinterpretation of how the different tokens work. In the more elaborate example from pastebin he actually tests both uppercase and lowercase y and deduces that, without argumentation, they should actually be the same (with which I obviously disagree :)).