Issues

ZF-12063: Zend_Date::subMonth() - Strange behavior on february

Description

Zend_Date offers different methods to help working with dates. One of them is the "subMonth" method.

I have been using this method to work trough dates and performing DB queries. For example:


$date = new Zend_Date('2012-03-01 00:00:00');
$lastMonth = $date->subMonth(1); //Expected value is '2012-02-01 00:00:00'

This works for every case, except when the date is 'YEAR-02-01 00:00:00'. In the case the date is February 1st, the date returned it January 1st of the previous year.


$date = new Zend_Date('2012-02-01 00:00:00');
$lastMonth = $date->subMonth(1); //Resulted value is '2011-01-01 00:00:00'

Interesting fact is if you continue to iterate trough this, the date gets back to normal, but it will fail in every February 1st.


$date = new Zend_Date('2012-02-01 00:00:00');
while ($months++ < 24) {
    echo $date->toString('YYYY-MM-dd') . '
'; $date->subMonth(1); }

This will output:


2012-02-01
2011-01-01 << Wrong year
2011-12-01
2011-11-01
...
2011-03-01
2011-02-01
2010-01-01 << Wrong year
2010-12-01
2010-11-01
...

I noticed others strange behaviors, but I was unable to replicate as this.

The file version is described as: * @version $Id: Date.php 22713 2010-07-29 11:41:56Z thomas $ It's the version included in the Magento library.

I have upgraded this component to the 1.11.10, and the problem seems to persists. * @version $Id: Date.php 24108 2011-06-04 00:09:27Z freak $

Comments

Added code example showing strange behavior during a while loop.

Added comment in the output block.

Reviewing the timestamps, they seems to be right. The problem might be related to the output.


[1328054400] => 2012-02-01 00:00:00
[1325376000] => 2011-01-01 00:00:00 << Wrong year
[1322697600] => 2011-12-01 00:00:00
[1320105600] => 2011-11-01 00:00:00
...
[1296518400] => 2011-02-01 00:00:00
[1293840000] => 2010-01-01 00:00:00 << Wrong year
[1291161600] => 2010-12-01 00:00:00
...
[1264982400] => 2010-02-01 00:00:00
[1262304000] => 2009-01-01 00:00:00 << Wrong year