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
Posted by Glauco Lins (gateon) on 2012-02-16T12:43:34.000+0000
Added code example showing strange behavior during a while loop.
Posted by Glauco Lins (gateon) on 2012-02-16T12:44:43.000+0000
Added comment in the output block.
Posted by Glauco Lins (gateon) on 2012-02-16T13:04:08.000+0000
Reviewing the timestamps, they seems to be right. The problem might be related to the output.