Link to home
Start Free TrialLog in
Avatar of Jamie
JamieFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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?

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
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jamie

ASKER

Hi Ferruccio,

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

regards

Jamie
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jamie

ASKER

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
Avatar of Jamie

ASKER

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

Regards

Jamie
Thanks for the points.  Glad to help.