Reputation: 472
I want how to extract the exact number of nights. I tried to achieve this by two ways but doesn't work. It return 20 nights but in real, it is 21 nights (March have 31 days)
$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));
Upvotes: 4
Views: 9013
Reputation: 1422
check this out for calculating nights
$start_ts = strtotime("2019-10-05");
$end_ts = strtotime("2019-10-06");
$diff = $end_ts - $start_ts;
echo round($diff / 86400);//1 night
work like charm
Upvotes: -2
Reputation: 4579
Use the new DateTime extension, 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");
Upvotes: 9