我想为我的网站创建计划。我怎样才能获得每周2天的时间表。示例:每周显示星期一和星期五。这是我的代码:
$day_of_week = "Monday";
$step = 1;
$unit = 'W';
$start = new DateTime("");
$end = clone $start;
$start->modify($day_of_week); // Move to first occurrence
$end->add(new DateInterval('P7W')); // Move to 1 year from start
$interval = new DateInterval("P{$step}{$unit}");
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo '<tr>';
echo '<td><a href="lihatAbsen.php?tanggal='.$date->format('d-m-Y').'&id='.$id_kuliah.'">'.$date->format('d M Y').'</td>';
}
$day_of_week2 = "Friday";
$start->modify($day_of_week2); // Move to first occurence
$period2 = new DatePeriod($start, $interval, $end);
foreach ($period2 as $date) {
echo '<td><a href="lihatAbsen.php?tanggal='.$date->format('d-m-Y').'&id='.$id_kuliah.'">'.$date->format('d M Y').'</td></tr>';
}
它正在工作,但我希望$period2
对象与<tr></tr>
对象打印在同一行$period
中。如何做正确的循环?或者输入日如何获得2天/周?
答案 0 :(得分:0)
您可以在其循环中操作第一个日期并将其添加到输出文本字符串
$monday=$date->format('d-m-Y');
$friday= date("d-m-Y",strtotime("$monday +4 days"));
答案 1 :(得分:0)
只需克隆当前的$date
对象并将其移至下周五。
$day_of_week = "Monday";
$step = 1;
$unit = 'W';
$start = new DateTime("");
$end = clone $start;
$start->modify($day_of_week);
$end->add(new DateInterval('P7W'));
$interval = new DateInterval("P{$step}{$unit}");
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo '<tr>';
echo '<td><a href="lihatAbsen.php?tanggal='.$date->format('d-m-Y').'&id='.$id_kuliah.'">'.$date->format('d M Y').'</td>';
$friday = clone $date;
$friday->modify('next Friday');
echo '<td><a href="lihatAbsen.php?tanggal='.$friday->format('d-m-Y').'&id='.$id_kuliah.'">'.$friday->format('d M Y').'</td></tr>';
}