此代码的作用:
它根据之前($date
)之前捕获的一天生成日期。
它总是计数一个星期。例如。如果您的开始日期($date
)为07.04.2019
,它将生成$final_amount
次的日期。
因此,如果日期的开始是07.04.2019
并且$final_amount
是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
我的代码存在问题
,如果假日不适合,我需要跳过该日期。所以如果21.04.2019
是假期,应跳过该假期,并在一周后以新日期替换。即使一个假期中有多个日期,它也应始终具有$final_amount
的金额。
我不知道如何实现此目标,因为我是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>';
答案 0 :(得分:1)
您可以使用while
循环而不是for
循环。如果满足条件,则可以跳过增加计数器的操作。
$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>';
}