how to calculate a difference between two Zend_Date objects, in months

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!


If I read the docs correctly, there's no implemented functionality for getting difference between 2 dates in seconds/minutes/.../months/years. So you need to calculate it yourself. Something like this will do (I don't know 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);