Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in Zend_Date (back in time)

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?

like image 712
Jeroen Weustink Avatar asked Aug 05 '10 09:08

Jeroen Weustink


1 Answers

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.

like image 79
zwippie Avatar answered Oct 28 '22 07:10

zwippie