Issues

ZF-11354: Zend_Dates adds months differently

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:


$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)."
"; $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)."
"; $date = new Zend_Date(1269900000); $date->add(12, Zend_Date::MONTH); echo "1 x 12 = ".$date->get(Zend_Date::ISO_8601);

Result:

 
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

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:


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