我有以下非常有效的脚本:
<?php
function create_time_range($start, $end, $interval = '30 mins', $format = '12') {
$startTime = strtotime($start);
$endTime = strtotime($end);
$returnTimeFormat = ($format == '12')?'g:i:s A':'G:i:s';
$current = time();
$addTime = strtotime('+'.$interval, $current);
$diff = $addTime - $current;
$times = array();
while ($startTime < $endTime) {
$times[] = date($returnTimeFormat, $startTime);
$startTime += $diff;
}
$times[] = date($returnTimeFormat, $startTime);
return $times;
}
$times = create_time_range('7:30', '18:30', '30 mins');
?>
<select name="time_picker">
<option value="">Select Time</option>
<?php foreach($times as $key=>$val){ ?>
<option value="<?php echo $val; ?>"><?php echo $val; ?></option>
<?php } ?>
</select>
不过,我希望对此进行更改:首先,它应该仅显示给定当前时间的可用时隙,其次应该包括2小时窗口作为“截止时间”。
实际示例和理想结果:让我们假设它是1PM,选择选项将是1.)甚至不显示1PM之前的值; 2。)仅显示下一个可用插槽为3PM(2小时窗口)
我已经为此苦苦挣扎了几天,但目前没有结果。一些专家的建议将不胜感激
答案 0 :(得分:0)
您可以这样做:
<?php
function create_time_range($start, $end, $interval = '30 mins', $format = '12') {
$startTime = strtotime($start);
$endTime = strtotime($end);
$returnTimeFormat = ($format == '12')?'g:i A':'G:i';
$hour = date('H');
$minute = (date('i')>30)?'30':'00';
$current = strtotime("$hour:$minute");
$addTime = strtotime('+'.$interval, $current);
$diff = $addTime - $current;
$times = array();
while ($current < $endTime) {
$times[] = date($returnTimeFormat, $current) . " " . "$hour:$minute";
$current += $diff;
}
$times[] = date($returnTimeFormat, $current);
return $times;
}
$times = create_time_range('8:00', '18:00', '30 mins');
?>
<select name="time_picker">
<option value="">Select Time</option>
<?php foreach($times as $key=>$val){ ?>
<option value="<?php echo $val; ?>"><?php echo $val; ?></option>
<?php } ?>
</select>
在这里,我将$ current用作基准时间,并通过获取当前小时数来创建该时间,然后将时间四舍五入到30分钟,然后从中创建时间。
希望这对您有帮助。