views:

128

answers:

1

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!

+2  A: 

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);
robertbasic
Sounds like a very rough solution.. The result won't be right..
Vincenzo
well, this example does give 25 for the end result... In the end, it is up to you to decide for the best answer =)
robertbasic