计算两个日期之间的晚数

时间:2016-12-16 11:37:05

标签: php date

我想如何提取确切的夜晚数量。我尝试通过两种方式实现这一目标,但是没有成功。它返回20晚,但实际上是21晚(3月有31天)

$startTimeStamp = strtotime("14-03-2017");                                   
$endTimeStamp = strtotime("04-04-2017");                                     

$timeDiff = abs($endTimeStamp - $startTimeStamp);                            

$numberDays = $timeDiff/86400;  // 86400 seconds in one day                  


$numberDays = intval($numberDays);                                           

echo $numberDays;                                                            

echo floor((strtotime('04-04-2017')-strtotime('14-03-2017'))/(60*60*24));    

2 个答案:

答案 0 :(得分:4)

使用新的DateTime扩展,PHP 5.3 +:

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

// this calculates the diff between two dates, which is the number of nights
$numberOfNights= $date2->diff($date1)->format("%a"); 

答案 1 :(得分:-1)

查看此内容以计算

$start_ts = strtotime("2019-10-05"); 
$end_ts = strtotime("2019-10-06"); 
$diff = $end_ts - $start_ts; 
echo round($diff / 86400);//1 night

像魅力一样工作

相关问题