问题
I have two objects of Zend_Date class and I want to calculate the difference between them in full calendar months.. How can I do this?
<?php
$d1 = new Zend_Date('1 Jan 2008');
$d2 = new Zend_Date('1 Feb 2010');
$months = $d1->sub($d2)->get(Zend_Date::MONTH);
assert($months == -25); // failure here
Thanks in advance!
回答1:
If I read the docs correctly, there's no implemented functionality for getting difference between 2 dates in seconds/minutes/.../months/years, so you'll need to calculate it yourself. Something like this will do (dunno if it takes leap years, DST and such into consideration):
<?php
$d1 = new Zend_Date('1 Jan 2008');
$d2 = new Zend_Date('1 Feb 2010');
$diff = $d1->sub($d2)->toValue();
$months = floor(((($diff/60)/60)/24)/30);
回答2:
With the help of Zend_Measure_Time you can easily get any difference you want:
$timeNow = new Zend_Date();
$timeThen = new Zend_Date("2011-05-21T10:30:00");
$difference = $timeNow->sub($timeThen);
$measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND);
$measure->convertTo(Zend_Measure_Time::MONTH);
echo $measure->getValue();
No need for complicated calculations!
回答3:
For anyone looking to get a friendly output (days, hours, minutes, seconds) I share my code here:
function _getDelay($since) {
$timeNow = new Zend_Date();
$timeThen = new Zend_Date($since);
$difference = $timeNow->sub($timeThen);
return $difference->toValue();
}
function _friendlySeconds($allSecs) {
$seconds = $allSecs % 60; $allMinutes = ($allSecs - $seconds) / 60;
$minutes = $allMinutes % 60; $allHours = ($allMinutes - $minutes) / 60;
$hours = $allHours % 24; $allDays = ($allHours - $hours) / 24;
return ($allDays > 0 ? $allDays . "d" : "") .
($hours > 0 ? $hours . "h" : "") .
($minutes > 0 ? $minutes . "m" : "") . $seconds . "s";
}
Just call this something like like:
echo "It happened " . _friendlySeconds(_getDelay('2010-11-18')) . " ago.";
回答4:
Thanks, your answer helped me solve the problem robertbasic, this is what I used to solve my code:
$dob = $post ['dob'];
$date = Zend_Date::now ();
$date3 = new Zend_Date ( "$dob", 'MM.dd.yyyy' );
$diff = $date->sub ( $date3 )->toValue ();
echo $age = floor ( ((($diff / 60) / 60) / 24) / 365 );
回答5:
You can get it using simple formula:
$diffInMonth = ($startYear == $endYear) ?
$endMonth - $startMonth + 1 :
(($endYear - $startYear) * 12) - $startMonth + $endMonth + 1;
回答6:
I used to do this by taking the difference between two Zend Dates but that is very complex as Elzo Valugi rightly remarks above. Better use the human approach. Your age in years is the difference between the year parts of both dates if your birth day has been already, if not, one year less. Something similar can be done with months.
function age($birthDate, $date)
{
$age = $date->get(Zend_Date::YEAR) - $birthDate->get(Zend_Date::YEAR);
$birthDay = clone $birthDate; // otherwise birthDate will be altered, objects are always passed by reference in PHP 5.3
$birthDay->set($date, Zend_Date::YEAR); // birthDay in the year of $date
if (1 == $BirthDay->compare($date)) {
$age = $age -1; // if birth day has not passed yet
}
return $age;
}
来源:https://stackoverflow.com/questions/3032101/how-to-calculate-a-difference-between-two-zend-date-objects-in-months