-1

What this code does:
It generates dates based on a single day that is grabbed before ($date). It counts always up one week. E.g. If your start date ($date) is 07.04.2019 it will generate the dates for $final_amount times.

So, if the start of the date is 07.04.2019 and $final_amount is: 5 it will output:

07.04.2019` (handled in separate code, as the first day is excluded in code!)
14.04.2019
21.04.2019
28.04.2019
05.04.2019

The issue with my code:
I need to skip the date if it falls into a holiday. So if e.g. 21.04.2019 is a holiday, it should be skipped and replaced with a new date one week later. It should always have the amount $final_amount even if one ore more dates are laying on a holiday. I have no Idea how this could be accomplished, as I am relatively new to PHP.

setlocale(LC_TIME, "de_DE"); //only necessary if the locale isn't already set
$date = new DateTime(helper('com://site/ohanah.date.format', array(
   'date' => $event->start,
   'format' => 'Y-m-d H:i',
   'timezone' => 'UTC'
)));

$date_scn = new DateTime(helper('com://site/ohanah.date.format', array(
   'date' => $event->end,
   'format' => 'H:i',
   'timezone' => 'UTC'
)));

$cnt = 2; // start the termin to count at two as the first one is already defined above
$raw_ticket_type = $event->ticket_types->name;
$filter_numbers = array_filter(preg_split('/\D/', $raw_ticket_type));
$filtered_numbers = reset($filter_numbers);
$first_occurence = substr($filtered_numbers[0], 0, 1);
$final_amount = $first_occurence - 1; // subtract 1 from $first_occurence as it is always one more

for ($i = 0; $i < $final_amount; $i++)
{ // loop
    $date->add(new DateInterval('P1W')); //add one week
    $formatted_time = utf8_encode(strftime("%A, %d. %B %Y, %H:%M", $date->getTimestamp()));
    $formatted_time_scnpart = utf8_encode(strftime("%H:%M", $date_scn->getTimestamp()));

    // This is the modal

    echo '<div class="termin-layout"><span class="termin-number-text">' . $cnt++ . '. ' . 'Termin' . '</span>
<span class="termin-date">' . $formatted_time . ' - ' . $formatted_time_scnpart . '</span></div>';
}

echo '</div></div></div>';
  • 1
    @RiggsFolly Your duplicate answers the title, but not the actual question. He doesn't want to exit the loop, he wants to skip that iteration. – Barmar Feb 04 '19 at 16:33
  • @Barmar Woops, my mistake – RiggsFolly Feb 04 '19 at 16:35
  • Please do not use `utf8_encode`. It is for the most part a very useless function. There are definitely better ways of doing what you want to do. – Dharman Oct 30 '19 at 09:24
  • What is the use of goes to operator in `$date-- > add(new DateInterval('P1W'));` Is this a typo? – Dharman Oct 30 '19 at 09:27
  • @Dharman yes, thanks for pointing this out. I fixed it with an edit. It looks like I copy and pasted this wrong time ago. This was not in the real code. –  Oct 30 '19 at 09:46

1 Answers1

1

You can use a while loop instead of the for loop. Then you can skip incrementing the counter if a condition is met.

$i = 0;
while ($i < $final_amount) {
    $date->add(new DateInterval('P1W'));
    if (is_holiday($date)) {
        continue;
    }
    $i++;
    $formatted_time = utf8_encode(strftime("%A, %d. %B %Y, %H:%M", $date->getTimestamp()));
    $formatted_time_scnpart = utf8_encode(strftime("%H:%M", $date_scn->getTimestamp()));

    // This is the modal

    echo '<div class="termin-layout"><span class="termin-number-text">' . $cnt++ . '. ' . 'Termin' . '</span>
<span class="termin-date">' . $formatted_time . ' - ' . $formatted_time_scnpart . '</span></div>';
}
Barmar
  • 741,623
  • 53
  • 500
  • 612