PHP下拉包含开始时间和结束时间

时间:2014-05-21 08:48:51

标签: php time

我目前正在为餐馆预订表格,但他们有不同的营业时间。我有一个小例子,我想象它会是什么。

我来自丹麦,所以不是上午10点和晚上10点,而是10点到22点。

<?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>

我真的希望有人可以帮助我。教程或视频也可以帮助很多。

4 个答案:

答案 0 :(得分:4)

您可以使用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);

答案 1 :(得分:0)

Slugonamission对。您可以创建一个数组来存储预订时间,然后合并一个for循环来遍历数组并增加所需的数量。希望这有助于人

答案 2 :(得分:0)

以下是代码:

<select>
<?php
$start_hour = 10;
$end_hour = 22;
$minutes_array = array("15", "30", "45");
for($i=$start_hour; $i<($end_hour + 1); $i++){
    $string = $i . ':00';
    echo '<option value="' . $string . '">' . $string . '</option>';
    if($i != $end_hour){
        for($j=0; $j<sizeof($minutes_array); $j++){
             $string = $i . ':' . $minutes_array[$j];
             echo '<option value="' . $string . '">' . $string . '</option>';
        }
    }
}
?>
</select>

这是一个不同的解决方案: 你将使用一个循环,但你必须自己编写数组。

<select>
<?php
$hours_array = array("10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45", "21:00", "21:15", "21:30", "21:45", "22:00");
for($i=0; $i<sizeof($hours_array); $i++){
    echo '<option value="' . $i. '">' . $i . '</option>';
}
?>
</select>

答案 3 :(得分:0)

<select>
    <?php 
        $tNow = strtotime('10:00');
        $end_time = strtotime('20:00');
        while($tNow <= $end_time){
            $optionvalue = date("H:i",$tNow);
            echo '<option value="'.$optionvalue 
              .'">'.$optionvalue.'</option>';
            $tNow = strtotime('+15 minutes',$tNow);
        }
    ?>
    </select>

或者您可以使用时间选择器

$('#time_at').timepicker({
     'timeFormat': 'H:i',
     'step': '15',  //15 Minute
     'minTime': '10:00',
     'maxTime': '22:00',
     'showDuration': true
});
<!-- Css Link -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-timepicker/1.10.0/jquery.timepicker.css" rel="stylesheet" type="text/css" />
<!-- js Link-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-timepicker/1.10.0/jquery.timepicker.js"></script>

<label>Time</label>
<input id="time_at" type="text" placeholder="HH:MM (Duration)">

相关问题