• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 120
  • Last Modified:

Looping through dates, but October 2016 displays Sun 30th Oct twice - but no Mon 31st Oct?! All other 2016 months displays fine.

Hello EE,

This has been driving me nuts, as I'm sure it is my error somewhere, but cannot work it out? Why does, when looping through with my php code,  1st to 31st October 2016, does it display Sun 30th October twice and no Mon 31st Oct?! All other 2016 months display fine?


echo '<br /><strong>October</strong><br />';
$date_from = strtotime('2016-10-01');
$date_to = strtotime('2016-10-31');

for ($i = $date_from; $i <= $date_to; $i+= 86400)
echo date("D", $i) . " " . date("jS", $i) . " " . date("M", $i) . "<br />";

Open in new window


Sat 1st Oct
Sun 2nd Oct
Mon 3rd Oct
Tue 4th Oct
Wed 5th Oct
Thu 6th Oct
Fri 7th Oct
Sat 8th Oct
Sun 9th Oct
Mon 10th Oct
Tue 11th Oct
Wed 12th Oct
Thu 13th Oct
Fri 14th Oct
Sat 15th Oct
Sun 16th Oct
Mon 17th Oct
Tue 18th Oct
Wed 19th Oct
Thu 20th Oct
Fri 21st Oct
Sat 22nd Oct
Sun 23rd Oct
Mon 24th Oct
Tue 25th Oct
Wed 26th Oct
Thu 27th Oct
Fri 28th Oct
Sat 29th Oct
Sun 30th Oct
Sun 30th Oct

Whereas November and December 2016 work fine;

Tue 1st Nov
Wed 2nd Nov
Thu 3rd Nov
Fri 4th Nov
Sat 5th Nov
Sun 6th Nov
Mon 7th Nov
Tue 8th Nov
Wed 9th Nov
Thu 10th Nov
Fri 11th Nov
Sat 12th Nov
Sun 13th Nov
Mon 14th Nov
Tue 15th Nov
Wed 16th Nov
Thu 17th Nov
Fri 18th Nov
Sat 19th Nov
Sun 20th Nov
Mon 21st Nov
Tue 22nd Nov
Wed 23rd Nov
Thu 24th Nov
Fri 25th Nov
Sat 26th Nov
Sun 27th Nov
Mon 28th Nov
Tue 29th Nov
Wed 30th Nov

Thu 1st Dec
Fri 2nd Dec
Sat 3rd Dec
Sun 4th Dec
Mon 5th Dec
Tue 6th Dec
Wed 7th Dec
Thu 8th Dec
Fri 9th Dec
Sat 10th Dec
Sun 11th Dec
Mon 12th Dec
Tue 13th Dec
Wed 14th Dec
Thu 15th Dec
Fri 16th Dec
Sat 17th Dec
Sun 18th Dec
Mon 19th Dec
Tue 20th Dec
Wed 21st Dec
Thu 22nd Dec
Fri 23rd Dec
Sat 24th Dec
Sun 25th Dec
Mon 26th Dec
Tue 27th Dec
Wed 28th Dec
Thu 29th Dec
Fri 30th Dec
Sat 31st Dec

What am I doing wrong?

Many thanks

  • 3
  • 2
2 Solutions
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Your code seems good.
Don't know why on 09-30-2016 there's that strange behaviour. Seem that the timestamp for that day has something wrong.

Anyway to get rid of this even in other months or years I'd do as follows:
echo '<br /><strong>October</strong><br />';
$date_from = strtotime('2016-10-01');
$date_to = strtotime('2016-10-31');
$count = floor(($date_to-$date_from) / 86400);
for ($i = 0; $i <= $count; $i++)
echo date('D jS M', strtotime(date('m/d/Y',$date_from)."+$i days"))."<br />";

Open in new window

JamieAuthor Commented:
Hi Ferruccio,

Yes, very strange - thank you for the workaround, it sorts the problem and is very much appreciated.


Brian TaoSenior Business Solutions ConsultantCommented:
Although this is already closed with an accepted solution, here's the reason:
Sun 30th Oct happens to be the end day of daylight saving time in London.  When you add 86400 seconds to it, it still falls on the same day as there are 25 hours in that specific day.
However when you add 1 day to it, it advances to the next day without "counting" the seconds, minutes and hours.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

JamieAuthor Commented:
Hi Brain,

Ah, many thanks for adding your comment, as it all makes sense now and explains what caused the problem and how the solution worked around it.

Much appreciated

JamieAuthor Commented:
Hi Ferruccio & Brian,

Many thanks, the help you both provided is very much appreciated and resolved my problem code - Ferruccio for the solution and Brian for explaining why my script wasn't working


Brian TaoSenior Business Solutions ConsultantCommented:
Thanks for the points.  Glad to help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now