我想用时间间隔显示时间拾取

时间:2015-08-04 08:58:28

标签: php date datetime time

我也获得了start timeend timebreak startbreak endtime interval

   $start_time='9:00';
   $end_time='15:00';
   $break_start='13:00';
   $break_end='14:00';
   $time_interval='60';

我想要的输出如下所示。用户从中选择时间段。

9:00 to 10:00
10:00 to 11:00
11:00 to 12:00
12:00 to 13:00
14:00 to 15:00

我想弄清楚这一点,但不知道该怎么做。

3 个答案:

答案 0 :(得分:4)

试试这个......

    $start_time='9:00';
    $end_time='15:00';
    $break_start='13:00';
    $break_end='14:00';
    $time_interval='60';
    for($i=strtotime($start_time);$i<=strtotime($end_time);$i=$i+(60*$time_interval))
    {
        if(strtotime($start_time) < $i)
        {
            $workstart=date('H:i',($i-(60*$time_interval)));
            $workend=date('H:i', $i);
            if(strtotime($break_start) !=$i-(60*$time_interval) && strtotime($break_end) !=$i)
            {
                echo $workstart.' to '.$workend;
                echo "</br>";
            }
        }
    }

<强>输出:

09:00 to 10:00
10:00 to 11:00
11:00 to 12:00
12:00 to 13:00
14:00 to 15:00

答案 1 :(得分:1)

<?php
     $start_time= new DateTime("9:00");
     $end_time= new DateTime("15:00");
     $break_start= new DateTime("13:00");
     $break_end= new DateTime("14:00");
     $time_interval= DateInterval::createFromDateString('60 min');

    $times    = new DatePeriod($start_time, $time_interval, $end_time);

foreach ($times as $time) {
    echo "<input type='radio'>";
    echo $time->format('H:i'), '-', 
         $time->add($time_interval)->format('H:i'), "\n";
         echo "<br/>";
}

?>

将您想要的名字放入收音机

答案 2 :(得分:1)

试试这个

<?php
$start_time='9:00';
$end_time='15:00';
$break_start='13:00';
$break_end='14:00';
$time_interval='60';

for($i=strtotime($start_time);$i<strtotime($end_time); $i=$i+($time_interval*60))
{
    if(!($i>=strtotime($break_start) && $i<strtotime($break_end)))
        echo date("H:i",$i).' to '.date("H:i",($i+($time_interval*60))).'<Br/>';        
}
?>