日期之间的天数不起作用...

Days between date do not working...

您好,我写了 php 代码来计算两个日期之间的天数。你能告诉我哪里出错了吗?

$today = date("Y-m-d");
$date = "2015-03-08";
$temp = date_create($date);
$datediff = $today - $temp;
$days = floor($datediff/(60*60*24));

您可以使用 phps 日期时间对象:

$today = new DateTime(); // Passing no argument defaults to today
$newYear= new DateTime('2016-01-01');
$interval = $today->diff($newYear);
$diff = $interval->format('%a');

echo $diff;

输出

5

如果你想要一个签名号码然后使用这个

$diff = $interval->format('%R%a');

输出

-5

所以你知道 $newYear 是 5 天 before $today.