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!
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.
With the help of Zend_Measure_Time you can easily get any difference you want:
No need for complicated calculations!
You can get it using simple formula:
For anyone looking to get a friendly output (days, hours, minutes, seconds) I share my code here:
Just call this something like like:
Thanks, your answer helped me solve the problem robertbasic, this is what I used to solve my code:
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):