Issues

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

Issue Type: Bug Created: 2012-02-16T12:36:26.000+0000 Last Updated: 2012-02-16T13:04:08.000+0000 Status: Open Fix version(s): Reporter: Glauco Lins (gateon) Assignee: Thomas Weidner (thomas) Tags: -

Related issues: Attachments:

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:

<pre class="highlight">
$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.

<pre class="highlight">
$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.

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

This will output:

<pre class="highlight">
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.

<pre class="highlight">
[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

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts