php - 我想用 am 或 pm 以 30 分钟为间隔显示时间

标签 php html mysql datetime time

我正在开发一些可用时间为上午 10:00 到下午 5:00 的预订系统。我在表格中以 30 分钟的间隔显示这个时间。上午 10:00 是开始时间,5 :00 pm 是结束时间。但我可以在下一行以 30 分钟的间隔显示时间,例如第一行的 10:00。第二行的 10:30 等等。但我想要上午 10:00 - 第排上午 10:30。第二排上午 10:30 - 11:00。

 <table style="border: 1px solid;">
    <thead>
      <tr>
        <td style="border:1px solid;padding:11px;">Time</td>
        <td style="border:1px solid;padding:11px;">Book</td>
      </tr>
    </thead>
    <tbody>
    <?php 
        $starttime = $query2['starttime'];  // your start time
        $endtime = $query2['endtime'];  // End time
        $duration = '30';  // split by 30 mins

       $array_of_time = array ();
       $start_time    = strtotime ($starttime); //change to strtotime
       $end_time      = strtotime ($endtime); //change to strtotime

       $add_mins  = $duration * 60;
       while ($start_time <= $end_time) // loop between time
  {
 $array_of_time[] = date ("h:i", $start_time);
 $start_time += $add_mins; // to check endtime
  }

  foreach ($array_of_time as $key => $single_time) { 
  ?>  
       <tr>
        <td style="border:1px solid;padding:11px;"><?php echo $single_time; ?><br /><span style="color:red;"><?php echo "Available"; ?></span></td>
        <td style="border:1px solid;padding:11px;"><input type="radio" value="<?php echo $single_time; ?>" id="A" name="A" ></td>
      </tr>
      <?php } ?>

      </tbody>
  </table>

我想在第一行显示上午 10:00 - 10:30 等时间。

最佳答案

您可以使用 DateTime 对象。这样您以后就可以通过替换 DateTime 构造函数中的值来修改它。

$date = new DateTime('2016-09-17 10:00');

while ( $start_time < $end_time )
{
    $start = $date->format('H:i');

    $date->add(new DateInterval('PT30M'));

    echo $start . ' - ' . $date->format('H:i') . "\n";

    $start_time += $add_mins;
}

关于php - 我想用 am 或 pm 以 30 分钟为间隔显示时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543644/

相关文章:

php - 将 x 小时 x 分钟前转换回 unix 时间戳

html - 当 flexbox 元素以列模式包装时,容器不会增加其宽度

javascript - 如何从 json 调用 HTML 文件以将其加载到模板中

mysql - Wampserver 登录失败 mysql windows 7

php - 在 php 中将 dd-mm-yyyy 转换为 yyyy-mm-dd

Php 更新查询不起作用

php - utf8 中的 'messed up characters' 列表

HTML5 文档类型将 IE9 置于怪癖模式?

mysql - Oracle 在 for 循环中提交

php - 哪个更快/更聪明,为什么是 : COUNT(*) or storing the numbers each the do something