我目前正在为一家餐厅制作预订表,但他们的营业时间不同。我有一个小例子,我想象它会是什么样子。
我来自丹麦,所以不是上午 10 点和晚上 10 点,而是 10:00 和 22:00。
<?php
// Opens at 10:00
$mondayStart = "10";
// Closes at 22:00
$mondayEnd = "22";
?>
<select>
// Here it is gonna generate times from 10:00 to 22:00 with 15 minutes apart like:
// <option>10:00</option>
// <option>10:15</option>
// <option>10:30</option>
// etc.
</select>
Run Code Online (Sandbox Code Playgroud)
我真的希望有人能帮助我。教程或视频也有很大帮助。
您可以使用strtotime函数来实现此目的。尝试下面的代码,它应该会给你你想要的。
$time_start = '10:00';
$time_end = '22:00';
# use date function with the time variables to create a timestamp to use in the while loop
$timestamp_start = strtotime(date('d-m-Y').' '.$time_start);
$timestamp_end = strtotime(date('d-m-Y').' '.$time_end);
# create array to fill with the options
$options_array = array();
# loop through until the end timestamp is reached
while($timestamp_start <= $timestamp_end){
$options_array[] = date('H:i', $timestamp_start);
$timestamp_start = $timestamp_start+900; //Adds 15 minutes
}
//Do with the options array as you wish
print_r($options_array);
Run Code Online (Sandbox Code Playgroud)