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?

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

	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.