Off-by-one bug
#186 Henry, Wednesday, 02 July 2014 4:55 AM (Category: Programming)
(Tags: programming bug)

Astute readers will have noticed that the code I put here recently for switching themes by time of day had an off-by-one bug. I was attempting to turn the day theme on at 7am and the night theme on at 7pm. The code was

$hour = (int) date ("G");
if ($hour < 7 || $hour > 19)
  $theme = "summer_night";
  $theme = "summer_pool";
print "

This worked well, except that the night theme was switching on at 8pm, not 7pm. However, as it is still light at 9pm in summer, I thought it appropriate to leave the code running as is, and change my notes on the league website to say that it changes at 8pm. Much easier.

If I really wanted to switch at 7am and 7pm, the if statement should be this:

if ($hour < 7 || $hour >= 19)