我需要一些改进我的任命制度的想法。
我有这个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
对如何实现有什么想法吗?任何帮助都将不胜感激!
发布于 2018-07-30 13:07:20
<?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
https://stackoverflow.com/questions/51586762
复制相似问题