0

The accepted answer in this question: Calculate business days is buggy. Just check

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

The problem is with this fragment:

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

So the minimal not working example is:

Why this expression:

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

Is not an integer for:

 $startDate = strtotime("2012-01-01");
 $endDate = strtotime("2012-05-01");
Community
  • 1
  • 1
mnowotka
  • 16,430
  • 18
  • 88
  • 134
  • 3
    why don't you ask that in the comments of the question you mentioned? Maybe it can help others landing there too ... – Luca Borrione Sep 19 '12 at 07:52
  • First because no one will look at it. Secondly because the problem is not with idea of this function but elsewhere. – mnowotka Sep 19 '12 at 07:53
  • What is the result of the expression? What timezone are you working in? – Luke Mills Sep 19 '12 at 07:54
  • Presumably because the clocks change between those dates for the timezone your using. But yeah, this really should be asked in the comments of that question. – John Carter Sep 19 '12 at 07:54

2 Answers2

3

You've crossed a DST threshold. Whenever you do so your duration will be (usually) one hour shorter or longer. If you wish to avoid this then work exclusively with UTC.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

As Ignacio says, it's because of passing the start (last Sunday of March) or end (last Sunday od October) of Daylight Saving Time... so you end up losing/gaining an hour and thus messing up the calculation.

All I added was round() to the equation and it deals with the situation perfectly :)

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