Calculate number of days between two dates with accuracy

Well, this may be a stone age question, but I got confused after reading so many questions here.

  • A simple solution using strtotime() has been pointed out here, but the last comment about time change for the accepted answer leads me to diff function.

  • Many of the links suggest me to diff function , something like Calculate number of hours between 2 dates in PHP.

  • But then I saw a PHP bug that diff shows wrong values when first date of the days are involved (link).

  • Also days part on diff function always return 6015 on Windows platform (link).

  • I read many more links and all of them ends with diff() function. Is there a reliable solution for this problem?

    Note: I am using PHP version 5.3.5


    If you don't want to use datediff neither strtotime, one more solution is to use getTimestamp and calc the difference

    $datetime1 = new DateTime('13-03-01');
    $datetime2 = new DateTime('13-04-01');
    
    $datetime1TS = datetime1->getTimestamp;
    $datetime2TS = datetime2->getTimestamp;
    
    $interval = ($datetime2TS - $datetime1TS)/(60*60*24);
    

    But i think that datediff is better.

    链接地址: http://www.djcxy.com/p/10116.html

    上一篇: php间隔和时区

    下一篇: 精确计算两个日期之间的天数