查找两个日期之间的天数

如何使用PHP查找两个日期之间的天数?


$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;

echo round($datediff / (60 * 60 * 24));

如果您使用PHP 5.3 > ,这是计算差异的最准确的方法:

$date1 = new DateTime("2010-07-06");
$date2 = new DateTime("2010-07-09");

$diff = $date2->diff($date1)->format("%a");

从PHP 5.3及更高版本开始,新的日期/时间函数被添加以获得不同:

$datetime1 = new DateTime("2010-06-20");

$datetime2 = new DateTime("2011-06-22");

$difference = $datetime1->diff($datetime2);

echo 'Difference: '.$difference->y.' years, ' 
                   .$difference->m.' months, ' 
                   .$difference->d.' days';

print_r($difference);

结果如下:

Difference: 1 years, 0 months, 2 days

DateInterval Object
(
    [y] => 1
    [m] => 0
    [d] => 2
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 367
)

希望能帮助到你 !

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

上一篇: Finding the number of days between two dates

下一篇: How to find the last day of the month from date?