POSTS

Are there 86400 seconds in a day?

There are 24 hours in a day, 60 minutes in an hour and 60 seconds in a minute.

So, there should be 24 * 60 * 60 = 86400 seconds in a day.

Seems fairly straightforward. Let’s put this to test. Let’s print the first 10 days of November 2016.

Here’s a simple way to do this:

date_default_timezone_set("America/New_York");
$date = strtotime("2016-11-01 00:00:00");

for ($i=0; $i < 10; $i++) {
  echo date("m/d/Y", $date) . "\n";
  $date += 86400;
}

Let’s run this and look at the output:

11/01/2016
11/02/2016
11/03/2016
11/04/2016
11/05/2016
11/06/2016
11/06/2016
11/07/2016
11/08/2016
11/09/2016

November 6th appears twice! How is that possible?

This is because in the first line of the code, I had set the timezone as America/New_York and New York observes Daylight saving time. In North America, every year on the second Sunday of March, the clock jumps from 01:59:59 to 03:00:00, thereby skipping an entire hour. This hour is later adjusted on the second Sunday of November when the clock goes from 01:59:59 back to 01:00:00, gaining back the hour that was lost in March. In other words, there were 25 hours on November 6th 2016.

To see exactly what is happening in our code, let’s modify it slightly and have it print the date and the time.

date_default_timezone_set("America/New_York");
$date = strtotime("2016-11-01 00:00:00");

for ($i=0; $i < 10; $i++) {
  echo date("m/d/Y H:i:s", $date) . "\n";
  $date += 86400;
}

And now the output reads:

11/01/2016 00:00:00
11/02/2016 00:00:00
11/03/2016 00:00:00
11/04/2016 00:00:00
11/05/2016 00:00:00
11/06/2016 00:00:00
11/06/2016 23:00:00
11/07/2016 23:00:00
11/08/2016 23:00:00
11/09/2016 23:00:00

We went from midnight to 11pm because of the extra hour in the day, and that is why November 6th appears twice.

Please bear in mind, countries in Europe have a different schedule - from the last Sunday in March to the last Sunday in October. To further complicate things, countries in Southern Hemisphere have yet another schedule.

If you wish to jump to the next date, use the strtotime() function with a relative offset, like this:

date_default_timezone_set("America/New_York");
$date = strtotime("2016-11-06 00:00:00");
$next_day = strtotime("+1 day", $date);
echo date("Y-m-d H:i:s", $next_day) . "\n";

And this will correctly give you:

2016-11-07 00:00:00

In conclusion, do not use 86400 as a measure of number of seconds in a day.