php数组只通过foreach循环显示最后一个条目

svmlkihl  于 2023-05-16  发布在  PHP
关注(0)|答案(1)|浏览(112)

我正在尝试显示预订日历的多个时间段。
我有一个'timeslots'函数,它通过start,end,cleanup和duration变量:

function timeslots($duration, $cleanup, $start, $end) {
  $start = new DateTime($start);
  $end = new DateTime($end);
  $interval = new DateInterval("PT".$duration."M");
  $cleanupInterval = new DateInterval("PT".$cleanup."M");
  $slots = array();

  for($intStart = $start; $intStart<$end; $intStart->add($interval)->add($cleanupInterval)) {
    $endPeriod = clone $intStart;
    $endPeriod->add($interval);
    if($endPeriod>$end) {
      break;
    }

    $slots = $intStart->format("H:iA")."-". $endPeriod->format("H:iA");
    print($slots);
  }

  return $slots;
}

在HTML中使用:

<div class="container">
        <h1 class="text-center">Book for Date: <?php echo date('m/d/Y', strtotime($date)); ?></h1><hr>
        <div class="row">
          <?php $timeslots[] = timeslots($duration, $cleanup, $start, $end);
          
          foreach($timeslots as $ts){          
          ?>
          <div class="col-md-2">
            <button class="btn btn-success"><?php echo $ts; ?></button>
          </div>
          <?php } ?>
        </div>
    </div>

我应该从上午9点到下午5点有10分钟的间隔。enter image description here
我只看到最后一个条目。我打印出函数中的$slots数组,以显示它确实包含所有条目。enter image description here

kxeu7u2r

kxeu7u2r1#

$slots[] = $intStart->format("H:iA")."-". $endPeriod->format("H:iA") ;

应该使用而不是

$slots = $intStart->format("H:iA")."-". $endPeriod->format("H:iA");

开启时隙功能

相关问题