Date Difference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jishcem
Forum Newbie
Posts: 12
Joined: Tue Nov 10, 2009 11:48 pm

Date Difference

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Date Difference

Post by requinix »

Google it.
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: Date Difference

Post 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
Post Reply