我有这个功能,可以在选择输入中为我提供一组选项。这些选项给了我 5 分钟间隔的时间。问题是当时间像 23:45 时,选项将从 00:10 开始并基于 $j 变量循环。
这就是我想要用文字做的:给我一个从 $open_time 到 $close_time 的 5 分钟间隔内的选项列表。如果当前时间 ($timeNow) 大于 $open_time,请将 $open_time 设置为 $timeNow 以显示为第一个选项。仅在 $close_time 之前执行此循环。
我希望这很清楚。感谢你的帮助 :)
这是代码:
function selectTimesofDay(){
$output = "";
$now = date('G:i ', time()); // time now
$timeNow = strtotime($now); // strtotime now
$next_five = ceil($timeNow / 300) * 300; // get next 5 minute
// time now rounded to next 10 minute
$round5minNow = date('G:i', strtotime('+15 minutes',$next_five));
$open_time = strtotime('17:00');
$close_time = strtotime('23:59');
// in the middle of working hours, time sets to current
if($timeNow >= $open_time && $timeNow < $close_time){
$open_time = strtotime($round5minNow);
}
$time_diff = round(($close_time - $open_time)/60) ;
if(date('l') == 'Friday'){
$j = ($time_diff/5)+11; // working hours extended untill 1:00 AM
} else{
$j = ($time_diff/5)-1; // working hours untill 12:00 AM
}
for($i = 0; $i <= $j; $i++){
$b = $i*5;
$data = date('l')." - ".date("H:i", strtotime('+'.$b.' minutes', $open_time));
$output .= "<option value=\"{$data}\">";
$output .= $data;
$output .= "</option>";
}
return $output;
}