moving companies : Computers : Programming : Languages : PHP : Function Libraries : Date and Time

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

32 of 156 people (21%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

How can I work out the difference (in number of days) between two dates in yyyy-mm-dd format?

Feb 3rd, 2000 15:19
Matt Gregory, piyush purang, Nathan Wallace,


<?
    //the 4 digit year + 2 digit month + 2 digit day is designed to be 
an ever-increasing number so we can use strcmp on them...
    $datestring1 = "2000-01-02";
    $datestring2 = "2000-01-30";
    $date1=explode("-",$d1);
    $date2=explode("-",$d2);
    sprintf($datestring1, "%s%s%s", $date1[0], $date1[1], $date1[2]);
    sprintf($datestring2, "%s%s%s", $date2[0], $date2[2], $date2[2]);
    //now just do your comparison...
    $result = strcmp($datestring1, $datestring2);
    if($result < 0)
        print("$datestring1 < $datestring2")
    else if($result > 0)
        print("$datestring1 > $datestring2")
    else
        print("$datestring1 = $datestring2")
    //NOTE: The integer value returned by the strcmp function is the
    //same as the value of a difference math function.
    //and this should work for all dates. It should only take minor
    //modifications to do the same for the difference in days, seconds,
    //hours, minutes years etc...
?>