所以,我有如下代码:
$dateBase = $amount->getDate();
for ($i = 1; $i <= 3; $i++) {
$installment = new Installment();
if ($i == 1) {
//do stuff
} else {
var_dump('1-'.$dateBase->toString());
$dateBase->addMonth(1);
var_dump('2-'.$dateBase->toString());
}
$installment->setDate($dateBase);
$dataBase = clone $dataBase;
}
输出为:
string(25) "1-Oct 1, 2014 12:00:00 AM"
string(25) "2-Dec 1, 2014 12:00:00 AM"
string(25) "1-Dec 1 , 2014 年 12:00:00 AM"
string(26) "2-Jan 31, 2015 12:00:00 AM"
您可以看到,在第二个循环中(当 $i=2 时),他增加了 2 个月而不是 1 个月
EDIT1:做更多的研究,我认为有时区错误,我改变了代码如下
$date = new Zend_Date('10/1/2014');
var_dump($date);
$date->addMonth(1);
var_dump($date, $dateBase);
$dateBase->addMonth(1);
var_dump($dateBase);
输出如下:
object(Zend_Date)#3754 (8) {
["_locale":"Zend_Date":private]=>
string(2) "en"
["_fractional":"Zend_Date":private]=>
int(0)
["_precision":"Zend_Date":private]=>
int(3)
["_unixTimestamp":"Zend_Date_DateObject":private]=>
string(10) "1412114400"
["_timezone":"Zend_Date_DateObject":private]=>
string(12) "Europe/Paris"
["_offset":"Zend_Date_DateObject":private]=>
int(-3600)
["_syncronised":"Zend_Date_DateObject":private]=>
int(0)
["_dst":protected]=>
bool(true)
}
object(Zend_Date)#3754 (8) {
["_locale":"Zend_Date":private]=>
string(2) "en"
["_fractional":"Zend_Date":private]=>
int(0)
["_precision":"Zend_Date":private]=>
int(3)
["_unixTimestamp":"Zend_Date_DateObject":private]=>
int(1414796400)
["_timezone":"Zend_Date_DateObject":private]=>
string(12) "Europe/Paris"
["_offset":"Zend_Date_DateObject":private]=>
int(-3600)
["_syncronised":"Zend_Date_DateObject":private]=>
int(0)
["_dst":protected]=>
bool(true)
}
object(Zend_Date)#3755 (8) {
["_locale":"Zend_Date":private]=>
string(2) "en"
["_fractional":"Zend_Date":private]=>
int(0)
["_precision":"Zend_Date":private]=>
int(3)
["_unixTimestamp":"Zend_Date_DateObject":private]=>
string(10) "1412114400"
["_timezone":"Zend_Date_DateObject":private]=>
string(9) "Etc/GMT-2"
["_offset":"Zend_Date_DateObject":private]=>
int(-7200)
["_syncronised":"Zend_Date_DateObject":private]=>
int(0)
["_dst":protected]=>
bool(true)
}
object(Zend_Date)#3755 (8) {
["_locale":"Zend_Date":private]=>
string(2) "en"
["_fractional":"Zend_Date":private]=>
int(0)
["_precision":"Zend_Date":private]=>
int(3)
["_unixTimestamp":"Zend_Date_DateObject":private]=>
string(10) "1417384800"
["_timezone":"Zend_Date_DateObject":private]=>
string(9) "Etc/GMT-2"
["_offset":"Zend_Date_DateObject":private]=>
int(-7200)
["_syncronised":"Zend_Date_DateObject":private]=>
int(0)
["_dst":protected]=>
bool(true)
}
您可以看到 $date 和 $dataBase 日期之间没有区别,只是时区,但是当我们向两者添加一个月时,它们的反应不同,$dateBase 被添加了额外的一个月