views:

18432

answers:

13

Hi,

I have to dates of the form

Start Date: 2007-03-24 End Date: 2009-06-26

Now I need to find the difference between these two in the below form:

2 years, 3 months and 2 days

Can someone please help me with this

+17  A: 

You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it's rather easy to calculate different time periods.

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);
Emil H
If you're using the DateTime class you can go for $date->format('U') to get the unix timestamp.
Jon Cram
It's not true if you have to deal with summer/winter time.In this particular case when you adjust summer/winter time, one day equals 23 or 25 hours.
Arno
Well, the same argument could be made for leap years. It doesn't take that into account either. Still, I'm not convinced that you even want to take that into account since we're discussing a range here. The semantics for a range are somewhat different than for an absolute date.
Emil H
A: 

Here you find a working function to do that: http://www.developertutorials.com/tutorials/php/calculating-difference-between-dates-php-051018/page1.html

You could find it searching in google the title of your question.

alexmeia
A: 

You can use the

getdate()

function which returns an array containing all elements of the date/time supplied:

$diff = abs($endDate - $startDate);
$my_t=getdate($diff);
print("$my_t[year] years, $my_t[month] months and $my_t[mday] days");

If your start and end dates are in string format then use

$startDate = strtotime($startDateStr);
$endDate = strtotime($endDateStr);

before the above code

Mark Pim
doesn't seem to work. I get a date at the begining of the timestamp era.
Sirber
A: 

2 years 94 days. Calculating the months, taking into account leap years, would be problematic. How accurate does this need to be?

dbasnett
A: 

I found your article on the following page, which contains a number of references for Php Date Time calculations.

Calculate the difference between two Dates (and time) using Php. The following page provides a range of different methods (7 in total) for performing date / time calculations using Php, to determine the difference in time (hours, munites), days, months or years between two dates.

See Php Date Time – 7 Methods to Calculate the Difference between 2 dates.

James - Php Development
+2  A: 

View Hours and Minuts and Seconds..

$date1 = "2008-11-01 22:45:00"; 

$date2 = "2009-12-04 13:44:01"; 

$diff = abs(strtotime($date2) - strtotime($date1)); 

$years   = floor($diff / (365*60*60*24)); 
$months  = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); 
$days    = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

$hours   = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); 

$minuts  = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); 

$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60)); 

printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds); 
khaldonno
A: 

$years = floor($diff / (365.25*60*60*24)); - is much better...

Jay
A: 

how many methods are to caliculate the date-diff in php?

priya
A: 

I don't know if you are using a PHP framework or not, but a lot of PHP frameworks have date/time libraries and helpers to help keep you from reinventing the wheel.

For example CodeIgniter has the timespan() function. Simply input two Unix timestamps and it will automatically generate a result like this:

1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes

http://codeigniter.com/user_guide/helpers/date_helper.html

Jakobud
+7  A: 

I suggest to use DateTime and DateInterval objects.

$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days "; 

read more php DateTime::diff manual

jurka
A: 
esc
A: 

You can find answer using this URL http://erandaisuru.blogspot.com/2009/10/hot-to-get-time-difference-using-php.html

Jayathissa
A: 

I voted for jurka's answer as that's my favorite, but I have a pre-php.5.3 version...

I found myself working on a similar problem - which is how I got to this question in the first place - but just needed a difference in hours. But my function solved this one pretty nicely as well and I don't have anywhere in my own library to keep it where it won't get lost and forgotten, so... hope this is useful to someone.

/**
 *
 * @param DateTime $oDate1
 * @param DateTime $oDate2
 * @return array 
 */
function date_diff_array(DateTime $oDate1, DateTime $oDate2) {
    $aIntervals = array(
        'year'   => 0,
        'month'  => 0,
        'week'   => 0,
        'day'    => 0,
        'hour'   => 0,
        'minute' => 0,
        'second' => 0,
    );

    foreach($aIntervals as $sInterval => &$iInterval) {
        while($oDate1 < $oDate2){ 
            $oDate1->modify('+1 ' . $sInterval);
            if ($oDate1 >= $oDate2) {
                $oDate1->modify('-1 ' . $sInterval);
                break;
            } else {
                $iInterval++;
            }
        }
    }

    return $aIntervals;
}

And the test:

$oDate = new DateTime();
$oDate->modify('+111402189 seconds');
var_dump($oDate);
var_dump(date_diff_array(new DateTime(), $oDate));

And the result:

object(DateTime)[2]
  public 'date' => string '2014-04-29 18:52:51' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'America/New_York' (length=16)

array
  'year'   => int 3
  'month'  => int 6
  'week'   => int 1
  'day'    => int 4
  'hour'   => int 9
  'minute' => int 3
  'second' => int 8

I got the original idea from here, which I modified for my uses (and I hope my modification will show on that page as well).

You can very easily remove intervals you don't want (say "week") by removing them from the $aIntervals array, or maybe adding an $aExclude parameter, or just filter them out when you output the string.

enobrev