dadamssg87 Posted November 11, 2011 Share Posted November 11, 2011 I wrote this bit of code to get the next six datetimes of a certain date. It was working perfectly until this week. <?php $start = "2011-11-06"; $num_days = 6; for ($i = 0; $i <= $num_days; $i += 1) { $stamp = strtotime($start) + ($i * 86400); echo date('l - n/d/Y - h:i a',$stamp)."<br/>"; } ?> The 2011-11-06 is a Sunday so i would expect to get the following: Sunday - 11/06/2011 - 12:00 am Monday - 11/07/2011 - 12:00 am Tuesday - 11/08/2011 - 12:00 am Wednesday - 11/09/2011 - 12:00 am Thursday - 11/10/2011 - 12:00 am Friday - 11/11/2011 - 12:00 am Saturday - 11/12/2011 - 12:00 am instead it is producing: Sunday - 11/06/2011 - 12:00 am Sunday - 11/06/2011 - 11:00 pm Monday - 11/07/2011 - 11:00 pm Tuesday - 11/08/2011 - 11:00 pm Wednesday - 11/09/2011 - 11:00 pm Thursday - 11/10/2011 - 11:00 pm Friday - 11/11/2011 - 11:00 pm How should i go about fixing this so it doesn't happen again? Quote Link to comment https://forums.phpfreaks.com/topic/250914-i-think-daylight-savings-time-screwed-up-my-little-function/ Share on other sites More sharing options...
Psycho Posted November 11, 2011 Share Posted November 11, 2011 Well, you are certainly doing it the hard way. No need to use strtotime() on the start data and then add a numeric value to it. strtotime() can easily add days to a date using "+n days". Anyway, to solve your problem, just set the start date to noon, $start = "2011-11-06 12:00:00"; $num_days = 6; for ($day = 0; $day <= $num_days; $day += 1) { $stamp = strtotime("{$start} +{$day}days"); echo date('l - n/d/Y - h:i a',$stamp)."<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/250914-i-think-daylight-savings-time-screwed-up-my-little-function/#findComment-1287263 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.