Reputation: 24542
I have this code:
<?php
$start = new Zend_Date("2011-09-06T10:00:00+02:00",Zend_Date::ISO_8601);
$end = new Zend_Date("2011-09-06T10:01:00+02:00",Zend_Date::ISO_8601);
echo $end->sub($start);
?>
In short: I create two dates, with a minute's difference between them. Then I print out the difference (subtraction) between them.
The result, however, is:
01-01-1970 02:01:00
Basically, what I understand from this behaviour is that Zend_Date operates on dates without taking timezone into consideration, and then puts the timezone back in the result. Of course, this means that the subtraction result is off by the value of the timezone (+2h in my case).
What's the best way to get around this?
Upvotes: 1
Views: 343
Reputation: 317177
Yes, when echo'ing a Zend_Date
, it will take your timezone into account. To get the difference formatted for GMT dates, you have to set the timezone explicitly:
$start = new Zend_Date("2011-09-06T10:00:00+02:00",Zend_Date::ISO_8601);
$end = new Zend_Date("2011-09-06T10:01:00+02:00",Zend_Date::ISO_8601);
echo $end->sub($start)->setTimezone('GMT')->get(Zend_Date::ISO_8601);
This would output: 1970-01-01T00:01:00+00:00
On a sidenote, if you do not need the dynamic localization features of Zend_Date it's best to avoid it in favor or PHP's native DateTime API. There is really no reason to use Zend_Date just because it exists in ZF. PHP's own DateTime API is faster and easier to use. Getting the time difference with the DateTime API would be
$start = new DateTime("2011-09-06T10:00:00+02:00");
$end = new DateTime("2011-09-06T10:01:00+02:00");
echo $start->diff($end)->format('%H:%I:%S');
which would output 00:01:00
Upvotes: 1