2016-12-16 120 views
1

我想要如何提取确切的夜晚数。我试图通过两种方式实现这一点,但不起作用。它返回20个晚上,但真实的,它是21晚(三月有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));  
+0

我认为应该+1你到任何计算天,因为输出任何日期根据您的要求是不会给你想要的输出 – rahulsm

+0

可能重复的[查找两个日期之间的天数](http://stackoverflow.com/questions/2040560/finding-the-number日期之间的日期) – Tiger

+1

[如何计算使用PHP的两个日期之间的差异?](http://stackoverflow.com/questions/676824/how-to-calcul使用-php) –

回答

5

使用新的日期时间延长,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"); 
+0

$ date1 = new \ DateTime(“2010-07-06”); $ date2 = new \ DateTime(“2010-07-07”); ||返回0 ...? –

相关问题