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
Date Difference
Moderator: General Moderators
Re: Date Difference
Google it.
- SimpleManWeb
- Forum Commoner
- Posts: 57
- Joined: Wed Dec 30, 2009 4:15 pm
- Location: New Hampshire, USA
Re: Date Difference
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
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
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/>";
Hope this helps