代码之家  ›  专栏  ›  技术社区  ›  Iustin Beceneagă

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

php
  •  0
  • Iustin Beceneagă  · 技术社区  · 6 年前

    我需要对我的预约制度提出一些改进意见。

    我有一个mysql查询结果,意思是空闲间隔时间:

    hours        freeend
    09:00:00     12:00:01
    14:00:00     15:00:01
    16:00:00     19:00:01
    

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

    这是我的显示自由间隔时间的php代码,但它不是100%正确。。。

    $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;
    }
    
    }
    

    它只返回两个结果:

    09:00
    16:00
    

    正确的做法是:

    09:00
    10:00
    16:00
    17:00
    

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   nice_dev    6 年前
    <?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/>";
    }
    

    输出

    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