Jump to content

I think daylight savings time screwed up my little function


dadamssg87

Recommended Posts

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?

Link to comment
Share on other sites

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/>";

	}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.