Hello - I am trying to get a looping date repeater to work but it is driving me crazy! It works for most dates but breaks for dates that originate in early November. I created a page that shows the code and the output for anyone to see what is going on. The page is here:

loop test

Here is the code, which is also on the page.

$counter=0;
$i=1; // TEST LOOP START
$sequence = $_POST['sequence']; // days
$repeat =  $_POST['repeat']; // number of records
$originaldate = $_POST['year'] ."-".$_POST['month'] ."-" . $_POST['day']; // BREAKS IN EARLY NOVEMBER!!

while($i<=$repeat)
{
$counter = $counter + 1;
$s = ($sequence * $counter);
$newdate = date('Y-m-d',(strtotime($originaldate) + (86400 * $s)));
echo $newdate . " : s= " . $s . " : i= " . $i . ": counter= " . $counter . "<br />";

$i++;  // END LOOPER
}

I also included the results of the variables in the output to help troubleshoot. Try November 3 to start and click submit to see what happens.

How can I fix this? Is my code any good? What can I do?!

Thanks for any asasistance!

I have tried this and it seems to work as expected, I think. Can you please explain what it is you are trying to do and what the desired and undesired results are that you are getting. This is probably a simple logical error.

I've run into problems with Daylight Saving Time in the past. Your method of adding 86400 as 1 day in seconds breaks down when it crosses a daylight savings boundary.
To further complicate things, date functions in PHP rely very heavily on the operating system hosting them and some OSs have bugs related to dates in this regard.

If your host server uses PHP 5.3 you can use the new Date classes such as DateInterval.

Otherwise, you can use strtotime() to add time to a date/time.

echo date('n-j-Y',strtotime('2010-11-03 +4 days'));

Good luck!

commented: This line of code fixed my error. +0

Otherwise, you can use strtotime() to add time to a date/time.

echo date('n-j-Y',strtotime('2010-11-03 +4 days'));

Thank you!

This fixed the error I was getting for those early dates in November regardless of the year. It was driving me mad but this adjusted line of code took care of it. I changed it on my loop.php page for further testing and all seems well. I am reluctant to use the dateinterval code in the event I need to use this code on servers not running PHP 5.3.

Thanks so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.