Hour Difference between 2 dates in Zend
i am using zend framework and i want to know the hour difference between 2 dates. I know how to do it in core php using strtotime开发者_运维问答.
But i want it in Zend, if at all possible.
Ex: These are my dates in datetime format:
2011-04-13 23:00:00
2011-04-14 15:45:00
It seems there is a sub()
method in Zend_Date
:
sub($date, $part = null, $locale = null)
Subtracts the$part
of$date
having a locale$locale
from the current object's date.
Using Zend_Date::HOUR
as $part should do the trick, I suppose :
$num_month = $firstDate->sub($secondDate, Zend_Date::HOUR);
As a sidenote : when working with strtotime()
, dates are reprensented using UNIX Timestamps -- which can only go from 1970 to 2038 (on 32 bits systems, at least)
It would probably wiser to work with the DateTime
class, which allows one to manipulate a (virtually) unlimited range of dates.
Why you want to have such function in Zend when you can do this thing with core PHP?
Just use core PHP.
Thanks, i could achieve a solution this way:
$firstDay = new Zend_Date('2011-04-13 23:00:00', 'YYYY-MM-DD h:i:s');
$lastDay = new Zend_Date('2011-04-14 15:45:00', 'YYYY-MM-DD h:i:s');
$diff = $lastDay->sub($firstDay)->toValue();
$days = ceil($diff/60/60);
Finally i could get the ceil value of 17, but could not get the exact difference (16h, 45min)
精彩评论