Zend_Date :: sub()和ISO_8601计算小时差错

时间:2012-05-11 23:23:33

标签: php zend-framework iso8601 zend-date

我想计算两个Zend_Date对象之间的时差(对于倒数计算器):

$now = new Zend_Date($now_datetime, Zend_Date::ISO_8601);
$end= new Zend_Date($end_datetime, Zend_Date::ISO_8601);
echo $now->getIso(); 
echo $end->getIso();

$expires=array();
$expires['expired']=false;
if($end->isEarlier($now)){
   $expires['expired']=true;
   return $expires;
}

$dif=$end->sub($now);
$expires['days']=($dif->getDay()->toValue()/(60*60*24));
$expires['hours']=($dif->getHour()->toValue()/(60*60));
$expires['minutes'] = $dif->getMinute()->toValue()/60;
$expires['seconds'] = $dif->getSecond()->toValue();

var_dump($expires);

$ now_datetime =' 2012-06-30 01:01:01 '和$ end_datetime =' 2012-06-30 23:59:59 ',结果是

2012-06-30T01:01:01+02:00
2012-06-30T23:59:59+02:00
//array
'expired' => boolean false
'days' => int 0
'hours' => int 22
'minutes' => int 58
'seconds' => int 58

没关系。

但For For $ now_datetime =' 2012-06-30 00:01:01 '和$ end_datetime =' 2012-06-30 23:59:59 ',结果是

2012-06-30T00:01:01+02:00
2012-06-30T23:59:59+02:00
//array
'expired' => boolean false
'days' => int 1
'hours' => int -1
'minutes' => int 58
'seconds' => int 58

并且不行。我希望'小时' 23 ,而不是 -1 ?!

我使用php 5.3,Zend_Framework 1.10运行MAMP。 这有什么问题? ISO_8601用于MySQL'datetime'数据,我不想更改为mktime()......

1 个答案:

答案 0 :(得分:1)

尝试设置

date_default_timezone_set('UTC');
你的index.php中的

..它会正常工作..