Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 895
yegor256 Avatar asked Jun 13 '10 11:06

yegor256


People also ask

How to get the difference between two dates?

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.

How to calculate the days between two dates in months?

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.

What is the time difference between two DateTime objects?

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.

How to see the difference between two dates in days in Python?

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.


1 Answers

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!

like image 176
Luksurious Avatar answered Oct 20 '22 16:10

Luksurious