Solved

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

Posted on 2016-10-02
4
49 Views
Last Modified: 2016-10-02
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?

date_default_timezone_set('Europe/London');

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


Gives;

October
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;

November
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

December
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

Jamie
0
Comment
Question by:Jamie
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 total points
ID: 41825261
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:
date_default_timezone_set('Europe/London');
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

0
 

Author Closing Comment

by:Jamie
ID: 41825482
Hi Ferruccio,

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

regards

Jamie
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 41825801
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.
2
 

Author Comment

by:Jamie
ID: 41825847
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

Jamie
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now