Calculating difference between 2 Zend_Date objects

2.5k Views Asked by At

I have 2 Zend_Date objects:

$d1 = new Zend_Date('2011-11-14 12:20:30');
$d2 = new Zend_Date('2012-11-16 13:40:10');

And I need to calculate difference. My output should be like this:

Years: 1, Months: 0, Days: 2, Hours: 1, Minutes: 19, Seconds: 40

I can do it with DateTime class and diff method. But my hoster has PHP version < 5.3. Can you help me how can I do it in Zend? Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

There is no implemented function to calculate the difference between two dates, unfortunately.

<?php
$diff = $d2->sub($d1)->toValue();
$days = floor($diff/60/60/24);
$months = floor($diff/60/60/24/30);
?>

This should help you get the variables you need.

0
On

You can get the timestamp of Zend_Date objects by $date->get(Zend_Date::TIMESTAMP). Then you can work with the normal PHP-functions to format your date like described in the PHP manual