doulouxun6756 2018-03-09 02:39
浏览 10
已采纳

如何在php中创建时间列表?

I have x.

x is minutes.

I have a string start_time like: 7:00

And I have a string end_time like: 14:00

how can print a list of time with increase x minutes for each loop?

I want print something like this:

if x = 30

7:00 - 7:30
7:30 - 8:00
...
13:30 - 14:00

I try do it with math functions in php like this:

$time = '7:00';
$mm = $hh = 0;
$str = explode(":",$time);
if(($str[1]+ $x) > 60)
{
...
}

but it there a more simple method? can date function in php do it?

  • 写回答

3条回答 默认 最新

  • doumu9019 2018-03-09 02:45
    关注

    You can use DatePeriod together with DateInterval to achieve this.

    Create two DateTime intervals with your start and end times, and a DateInterval instance with the number of minutes you need. Then, create a DatePeriod with this information, and iterate over it to show the resulting times:

    <?php
    $minutes = 15;
    $start = "07:00";
    $end = "14:00";
    $startDate = DateTime::createFromFormat("H:i", $start);
    $endDate = DateTime::createFromFormat("H:i", $end);
    $interval = new DateInterval("PT".$minutes."M");
    $dateRange = new DatePeriod($startDate, $interval, $endDate);
    foreach ($dateRange as $date) {
        echo $date->format("H:i")."<br>";
    }
    

    Demo

    Result

    07:00

    07:15

    07:30

    07:45

    08:00

    08:15

    08:30

    08:45

    09:00

    09:15

    09:30

    09:45

    10:00

    10:15 // etc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改