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!
The timedelta represents a duration which is the difference between two dates, time, or datetime instances, to the microsecond resolution. To get a result in seconds, use the timedelta.seconds attribute Also, see the difference between the two dates in months. There are cases in which you receive dates in a date object instead of a string.
To calculate your data in the month format, you need to add the letter m as an argument at the end of your formula. Follow this format to calculate the days between two dates in months: The function will extract data from the referred cells to calculate the difference in months. You can also count the days between two dates in years.
The time difference is 748 days, 22 hours, and 3 minutes. If you need more explanations on how to quantify the time difference between two datetime objects, you may have a look at the following video of the YouTube channel of Kyle Monson.
Also, see the difference between two dates in days in Python. Python dateutil module provides a relativedelta class, representing an interval of time. For example, we can find the difference between two dates in year, months, days, hours, minutes, seconds, and microseconds using the relativedelta class.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With