PHP - the total number of days is not an integer

The accepted answer in this question is: Calculating working days is a mistake. Just check

echo getWorkingDays("2012-01-01","2012-05-01",$holidays); 

The problem is this snippet:

 $days = ($endDate - $startDate) / 86400 + 1; 

So a minimal non-working example:

Why is this expression:

 ($endDate - $startDate) / (60*60*24); 

Not an integer for:

  $startDate = strtotime("2012-01-01"); $endDate = strtotime("2012-05-01"); 
0
date php datetime
Sep 19
source share
2 answers

You have crossed the DST threshold. Whenever you do this, your duration will be (usually) one hour shorter or longer. If you want to avoid this, then work exclusively with UTC.

+3
Sep 19
source share

According to Ignacio, this is due to the passage of the start (last Sunday of March) or the end (last Sunday of October) from daylight saving time ... so you end up losing / gaining an hour and thus ruining the calculation.

Everything I added was rounded () to the equation and does a great job :)

 $days = round( (strtotime($endDate) - strtotime($startDate)) / 86400 + 1); 
0
Feb 22 '13 at 17:09
source share



All Articles