phpzend-frameworkiso8601zend-date

Zend_Date::sub() and ISO_8601 calculating hour difference wrong


I want to calculate time difference between two Zend_Date objects (for countdown calculator):

$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);

For $now_datetime ='2012-06-30 01:01:01' and $end_datetime='2012-06-30 23:59:59', the result is

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

and it is OK.

But for For $now_datetime ='2012-06-30 00:01:01' and $end_datetime='2012-06-30 23:59:59', the result is

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

and it is NOT OK. I expect 'hours' to be 23, not -1 ?!

I am running MAMP with php 5.3, Zend_Framework 1.10. What is wrong with that? ISO_8601 is used for MySQL 'datetime' data and I don't wanto to change to mktime()...


Solution

  • try to set

    date_default_timezone_set('UTC');
    

    in your index.php.. it will work fine..