I'm using the following function to check the difference in days between two dates
function calcNumberOfNights ($checkin, $checkout)
{
$checkinDate = new DateTime($checkin);
$checkoutDate = new DateTime($checkout);
$interval = $checkoutDate->diff($checkinDate);
return $interval->days;
}
To test it, I'm using the following code
$in = '2016-10-15';
$out = '2016-10-18';
var_dump(calcNumberOfNights($in,$out));//print '3'
All works fine, except when I try to calculate the interval between Oct 16th and Oct 18th.
$in = '2016-10-16';
$out = '2016-10-18';
var_dump(calcNumberOfNights($in,$out)); //print '1'
It is returning 1, when the correct output in this case should be '2'.
I think that the problem is in the timezone. It use the Brazilian timezone and Oct 16th of 2016 is the first day of summer time in Brazil (plus one hour).
My question is: How can I avoid this type of mistake but keep my default timezone?
I searched for a similar problem here in SO, but didn't found anything.