Page 1 of 1

Date Difference

Posted: Sat Jan 16, 2010 6:06 am
by jishcem
Hi, I would like to have the difference between two dates in years and months and days and minutes and seconds( Of course in php ). Any help is very much appreciated.

Thanks Ajeesh

Re: Date Difference

Posted: Sat Jan 16, 2010 8:00 am
by requinix
Google it.

Re: Date Difference

Posted: Sat Jan 16, 2010 9:50 am
by SimpleManWeb
Or, we could just help him here since he's already asked the question. After all, that's the point of this forum, right?

Ajeesh, the code would be something like this

Code: Select all

 
    $Date1 = "2000-02-23";
    $Date2 = "2010-01-16";
    $Date1Array = explode("-", $Date1);
    $Date2Array = explode("-", $Date2);
    $D1  = mktime(0, 0, 0, $Date1Array[1]  , $Date1Array[2], $Date1Array[0]);
    $D2  = mktime(0, 0, 0, $Date2Array[1]  , $Date2Array[2], $Date2Array[0]);
    $Difference = $D2 - $D1;
    $NewDate = date("Y-m-d", $Difference);
    echo "Years Between: ".((date("Y", $Difference)) - 1970)."<br/>";
    echo "Months Between: ".date("m", $Difference)."<br/>";
    echo "Days Between: ".date("d", $Difference)."<br/>";
 
Then, just use all of the difference date variables to gather any data you want. Note: you need the (-1970) for the year difference because mktime only goes back to 1970. If it were able to start at 0 then we wouldn't need it.

Hope this helps