首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >两个日期之间的所有可能日期,间隔为60分钟或可变间隔

两个日期之间的所有可能日期,间隔为60分钟或可变间隔
EN

Stack Overflow用户
提问于 2018-07-30 11:53:09
回答 1查看 51关注 0票数 0

我需要一些改进我的任命制度的想法。

我有这个mysql查询结果,这意味着空闲间隔时间:

代码语言:javascript
复制
hours        freeend
09:00:00     12:00:01
14:00:00     15:00:01
16:00:00     19:00:01

和时间间隔,假设是120分钟。(存储在变量$d中)

这是我的显示空闲间隔时间的php代码,但它不是100%正确的…

代码语言:javascript
复制
$d = 120; // let's say
$nr = $result->num_rows;

while($row = mysqli_fetch_array($result)) {

$start    = new DateTime($row['hours']);
$end      = new DateTime($row['freeend']); // add 1 second because last one is not included in the loop

if ($nr < 2) {$interval = new DateInterval('PT60M');} else {$interval = new DateInterval('PT'.$d.'M');}


$period   = new DatePeriod($start, $interval, $end);

$previous = '';
foreach ($period as $dt) {
    $current = $dt->format("H:i");
    if (!empty($previous)) {

        echo "<label class='btn btn-secondary bhours btn-lg'><input type='radio' name='hours' value='{$previous}' id='{$previous}'>{$previous}</label>";
    }
    $previous = $current;
}

}

它只返回两个结果:

代码语言:javascript
复制
09:00
16:00

正确的做法是:

代码语言:javascript
复制
09:00
10:00
16:00
17:00

对如何实现有什么想法吗?任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-30 13:07:20

代码语言:javascript
复制
<?php 


$arr = [
            [
                'hours' => '09:00:00',
                'freeend' => '12:00:01'
            ],
            [
                'hours' => '14:00:00',
                'freeend' => '15:00:01'
            ],
            [
                'hours' => '16:00:00',
                'freeend' => '19:00:01'
            ],
            [
                'hours' => '10:00:00',
                'freeend' => '23:00:01'
            ]
        ];

$appointment_duration = new DateInterval('PT2H');
$next_hour = new DateInterval('PT1H');

foreach($arr as $row){
    $start_time = new DateTime($row['hours']);
    $end_time   = new DateTime($row['freeend']);

    echo "Appointments available between $row[hours] and $row[freeend] <br/>";

    $curr_start_time = $start_time;
    $curr_end_time   = new DateTime($start_time->format("H:i:s"));
    $curr_end_time   = $curr_end_time->add($appointment_duration);

    do{
        if($curr_end_time > $end_time){
            echo "$row[hours]-$row[freeend] <br/>";
            break;
        }

        echo $curr_start_time->format("H:i:s"),"-",$curr_end_time->format("H:i:s"),"<br/>";
        $curr_start_time = $curr_start_time->add($next_hour);
        $curr_end_time   = new DateTime($curr_start_time->format("H:i:s"));
        $curr_end_time   = $curr_end_time->add($appointment_duration);

    }while($curr_end_time <= $end_time);    

    echo "<br/>";
}

输出

代码语言:javascript
复制
Appointments available between 09:00:00 and 12:00:01 
09:00:00-11:00:00
10:00:00-12:00:00

Appointments available between 14:00:00 and 15:00:01 
14:00:00-15:00:01 

Appointments available between 16:00:00 and 19:00:01 
16:00:00-18:00:00
17:00:00-19:00:00

Appointments available between 10:00:00 and 23:00:01 
10:00:00-12:00:00
11:00:00-13:00:00
12:00:00-14:00:00
13:00:00-15:00:00
14:00:00-16:00:00
15:00:00-17:00:00
16:00:00-18:00:00
17:00:00-19:00:00
18:00:00-20:00:00
19:00:00-21:00:00
20:00:00-22:00:00
21:00:00-23:00:00
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51586762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档