Issues

ZF-11354: Zend_Dates adds months differently

Issue Type: Bug Created: 2011-05-10T07:59:02.000+0000 Last Updated: 2011-05-27T12:47:13.000+0000 Status: Open Fix version(s): Reporter: Stephan van der Horn (stephan88) Assignee: Thomas Weidner (thomas) Tags: - Zend_Date

  • add
  • month
  • zend_date

Related issues: Attachments:

Description

I found a bug in Zend_Date where months get added differently when you add one month twelve times compared to adding three months four times (for example). I think the code below speaks for itself.

The PHP code:

<pre class="highlight">
$date = new Zend_Date(1269900000);
for($i = 0; $i < 12; $i++) {
    $date->add(1, Zend_Date::MONTH);
}
echo "12 x 1 = ".$date->get(Zend_Date::ISO_8601)."<br></br>";

$date = new Zend_Date(1269900000);
for($i = 0; $i < 4; $i++) {
    $date->add(3, Zend_Date::MONTH);
}
echo "4 x 3 = ".$date->get(Zend_Date::ISO_8601)."<br></br>";

$date = new Zend_Date(1269900000);
$date->add(12, Zend_Date::MONTH);
echo "1 x 12 = ".$date->get(Zend_Date::ISO_8601);

Result:

<pre class="highlight"> 
12 x 1 = 2011-03-28T00:00:00+02:00
4 x 3 = 2011-03-30T00:00:00+02:00
1 x 12 = 2011-03-30T00:00:00+02:00

Does anyone know a solution for this problem?

Comments

Posted by Andrey Shevchenko (distdev) on 2011-05-27T12:47:13.000+0000

It's not a bug, because on 11th iteration you have February 28th (according to start date - 30th and option extend_month == false). So, on next iteration you have March, 28th. I would suggest to replace your code smth like this:

<pre class="highlight">
$monthCount = 0;
for($i = 0; $i < 12; $i++) {
    $monthCount++;
}
$date->add($monthCount, Zend_Date::MONTH);

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