0

I have an event entry form that allows you to set an event to recur on a monthly basis on a specified date. Everything works just fine until you set it to stop in December. When you save it, it looks like to loads for a while but then eventually crashes.

Setting it to stop any other month works perfectly fine an updates instantly. It returns a 500 error so I don't think it's a timeout problem (This site returns a timeout notice when that's the case.).

Here's the code

if ($_REQUEST["Daily"] == 'numbered') {
    $endMonth = $_REQUEST['month'];
    $exd = $_REQUEST['everyXdays'];
    $inter = '-1';
    if (($_REQUEST['everyXdays'] != '-1') && ($_REQUEST['everyXdays'] != $date1->format('d'))) {
        $date1->setDate($date1->format('Y'),(int) $_REQUEST['month'],(int) $_REQUEST['everyXdays']);
    }
    for($date; $date->format('m') <= $endMonth; $date->add(new DateInterval('P1M'))) {
        $eventDays[] = $_REQUEST['everyXdays'] < '0'
            ? $date->format('Y-m-t')
            : $date->format('Y-m-').str_pad($_REQUEST['everyXdays'],2,"0",STR_PAD_LEFT);
    }
}

The code creates an array of events that get inserted into the database as child events. The original parent is then set to the final instance of the event. Basically the parent is moved to the end of the date range and creates child instances behind it.

The second IF statement says if 'everyXdays' is not the same as the parent's original start date, set the day as 'everyXdays'.

The FOR loop is what creates the event array. $date is the original start date that increments on each loop and $date1 is the final event instance's date.

A function then inserts them all into a database. I'm not including the function because, like I said earlier, it works perfectly fine when it's set to any other month. So I'm sure that's not the problem.

arch649
  • 1
  • 5

2 Answers2

0

I'm not sure, as I cannot run php code now, but I think, if you choose December, $endMonth gets the value 11, but $date->format('m') is 12, so the loop does not run a single time.
Can you please check for this bug?

elias
  • 849
  • 13
  • 28
0

FIXED: The for loop was only comparing months. So when the loop would tick over to January, it would only compare January to December. Since Jan < Dec, the loop would continue. Changing the for condition from $date->format('m') <= $endMonth; to $date->format('Y-m') <= $date1->format('Y-').$endMonth; that factors in the year. Now it stops when $date enters the new year.

arch649
  • 1
  • 5