How to skip a date if it is generated in a loop?
这段代码的作用:
它根据在 (
它总是计数一周。例如。如果您的开始日期 (
所以,如果日期的开始是
1 2 3 4 5 | 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 |
我的代码的问题:如果它属于假期,我需要跳过日期。所以如果例如
我不知道如何实现这一点,因为我对 PHP 比较陌生。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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 '<span class="termin-number-text">' . $cnt++ . '. ' . 'Termin' . '</span> <span class="termin-date">' . $formatted_time . ' - ' . $formatted_time_scnpart . '</span>'; } echo ''; |
您可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $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 '<span class="termin-number-text">' . $cnt++ . '. ' . 'Termin' . '</span> <span class="termin-date">' . $formatted_time . ' - ' . $formatted_time_scnpart . '</span>'; } |