Finding the number of days between two dates
如何使用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");
From PHP Version 5.3 and up, new date/time functions have been added to get difference:
$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);
Result as below:
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
)
Hope it helps !
链接地址: http://www.djcxy.com/p/57292.html上一篇: 垂直显示选项卡式文档而不是水平
下一篇: 查找两个日期之间的天数