Bug in Zend_Date (back in time)
Asked Answered
D

2

5

I have a very strange problem, Zend_Date is converting my timestamp to a year earlier.

In my action:

// Timestamp
$intTime = 1293922800;

// Zend_Date object
$objZendDate = new Zend_Date($intTime);

// Get date

echo date('Y-m-d',$intTime).'<br>';
echo $objZendDate->get('YYYY-MM-dd');

This outputs: 2011-01-02 2010-01-02

Can anyone tell me what i'm doing wrong?

Daughterinlaw answered 5/8, 2010 at 9:37 Comment(0)
M
13

From the ZF issue tracker it seems this is a known issue:

Recently a lot of ZF users are filing a bug that Zend_Date returns the wrong year, 2009 instead of 2008. This is however expected behaviour, and NOT A BUG!

From the FAQ:

When using own formats in your code you could come to a situation where you get for example 29.12.2009, but you expected to get 29.12.2008.

There is one year difference: 2009 instead of 2008. You should use the lower cased year constant. See this example:

$date->toString('dd.MM.yyyy');

instead of

$date->toString('dd.MM.YYYY');

From the manual

Note that the default ISO format differs from PHP's format which can be irritating if you have not used in previous. Especially the format specifiers for Year and Minute are often not used in the intended way.

For year there are two specifiers available which are often mistaken. The Y specifier for the ISO year and the y specifier for the real year. The difference is small but significant. Y calculates the ISO year, which is often used for calendar formats. See for example the 31. December 2007. The real year is 2007, but it is the first day of the first week in the week 1 of the year 2008. So, if you are using 'dd.MM.yyyy' you will get '31.December.2007' but if you use 'dd.MM.YYYY' you will get '31.December.2008'. As you see this is no bug but a expected behaviour depending on the used specifiers.

For minute the difference is not so big. ISO uses the specifier m for the minute, unlike PHP which uses i. So if you are getting no minute in your format check if you have used the right specifier.

Mesolithic answered 5/8, 2010 at 10:5 Comment(1)
I wish I would have seen this 4 hours ago!Ambry
M
0

To add to zwip's answer, what happens behind the scenes is that your date format YYYY-MM-dd is actually translated into o\-m\-d, which is then passed to PHP's date() function internally with the timestamp you provided.

Like mentioned in the other answer, and in the documentation for the o format on the date format page, the calculation of the year based on the ISO week can sometimes result in the year being one different to the value that you expect.

Macrophysics answered 5/8, 2010 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.