开发者

Display list of times from 12PM to 5AM (Loop)

开发者 https://www.devze.com 2023-04-06 22:00 出处:网络
What is good way display a list of times from 12PM to 5AM using Loop (PHP)? I don\'t think storing times in the array is good idea, there must be a better way.

What is good way display a list of times from 12PM to 5AM using Loop (PHP)?

I don't think storing times in the array is good idea, there must be a better way.

Like this as example:

    <select id="closetime">
        <option value="12:00:00"> 12:00 PM</option> 
        <option value="12:30:00"> 12:30 PM</option>
        <option value="13:00:00"> 13:00 PM</option> 
        <option value="13:30:00"> 13:30 PM</option> 
        <option value="14:00:00"> 14:00 PM</option> 
        <option value="14:30:00"> 14:30 PM</option> 
        <option value="15:00:00"> 15:00 PM</option> 
        <option value="15:30:00"> 15:30 PM</option> 
        <option value="16:00:00"> 16:00 PM</option> 
        <option value="16:30:00"> 16:30 PM</option> 
        <option value="17:00:00"> 17:00 PM</option> 
        <option value="17:30:00"> 17:30 PM</option> 
        <option value="18:00:00"> 18:00 PM</option>
        <option value="18:30:00"> 18:30 PM</option>
        <option value="19:00:00"> 19:00 PM</option>
        <option value="19:30:00"> 19:30 PM</option>
        <option value="20:00:00"> 20:00 PM</option>
        <option value="20:30:00"> 20:30 PM</option>
        <option value="21:00:00"> 21:00 PM</option>
        <option value="21:30:00"> 21:30 PM</option>
        <option 开发者_开发知识库value="22:00:00"> 22:00 PM</option>
        <option value="22:30:00"> 22:30 PM</option>
        <option value="23:00:00"> 23:00 PM</option>
        <option value="23:30:00"> 23:30 PM</option>
        <option value="00:00:00"> 00:00 AM</option>
        <option value="00:30:00"> 00:30 AM</option>
        <option value="01:00:00"> 01:00 AM</option>
        <option value="01:30:00"> 01:30 AM</option>
        <option value="02:00:00"> 02:00 AM</option>
        <option value="02:30:00"> 02:30 AM</option>
        <option value="03:00:00"> 03:00 AM</option>
        <option value="03:30:00"> 03:30 AM</option>
        <option value="04:00:00"> 04:00 AM</option>
        <option value="04:30:00"> 04:30 AM</option>
        <option value="05:00:00"> 05:00 AM</option>
    </select>

00:00:00 mean midnight in MySQL right?


I believe this is the easiest... and shortest way :: only 6 lines of code ;)

$start_time = "13:00:00";
$end_time = "05:00:00";
while(strtotime($start_time) >= strtotime($end_time)){?>
    <option value="<?=date("H:i:s", strtotime($start_time))?>"> <?=date("H:i A", strtotime($start_time))?></option>
    <? $start_time = date("H:i:s", strtotime("$start_time -30 minutes"));
}//end while


<select id="closetime">
<?php

for ($i = 12; $i < 30; $i++) {
    $num = $i > 23 ? $i - 24 : $i;
    $num = $num < 10 ? "0$num" : $num;
    $ampm = $num > 11 && $num < 24 ? 'PM' : 'AM';
    echo "<option value=\"$num:00:00\"> $num:00 $ampm</option>\n";
    if ($num != 5)
        echo "<option value=\"$num:30:00\"> $num:30 $ampm</option>\n";
}

?>
</select>

http://codepad.org/fCXU1Goh

OUTPUT

<select id="closetime">
<option value="12:00:00"> 12:00 PM</option>
<option value="12:30:00"> 12:30 PM</option>
<option value="13:00:00"> 13:00 PM</option>
<option value="13:30:00"> 13:30 PM</option>
<option value="14:00:00"> 14:00 PM</option>
<option value="14:30:00"> 14:30 PM</option>
<option value="15:00:00"> 15:00 PM</option>
<option value="15:30:00"> 15:30 PM</option>
<option value="16:00:00"> 16:00 PM</option>
<option value="16:30:00"> 16:30 PM</option>
<option value="17:00:00"> 17:00 PM</option>
<option value="17:30:00"> 17:30 PM</option>
<option value="18:00:00"> 18:00 PM</option>
<option value="18:30:00"> 18:30 PM</option>
<option value="19:00:00"> 19:00 PM</option>
<option value="19:30:00"> 19:30 PM</option>
<option value="20:00:00"> 20:00 PM</option>
<option value="20:30:00"> 20:30 PM</option>
<option value="21:00:00"> 21:00 PM</option>
<option value="21:30:00"> 21:30 PM</option>
<option value="22:00:00"> 22:00 PM</option>
<option value="22:30:00"> 22:30 PM</option>
<option value="23:00:00"> 23:00 PM</option>
<option value="23:30:00"> 23:30 PM</option>
<option value="00:00:00"> 00:00 AM</option>
<option value="00:30:00"> 00:30 AM</option>
<option value="01:00:00"> 01:00 AM</option>
<option value="01:30:00"> 01:30 AM</option>
<option value="02:00:00"> 02:00 AM</option>
<option value="02:30:00"> 02:30 AM</option>
<option value="03:00:00"> 03:00 AM</option>
<option value="03:30:00"> 03:30 AM</option>
<option value="04:00:00"> 04:00 AM</option>
<option value="04:30:00"> 04:30 AM</option>
<option value="05:00:00"> 05:00 AM</option>
</select>


I think the easiest method is:

for ($x = 12; $x <= 29; $x++) {
  $H = $x % 24;
  $ap = ($H < 12)? "AM" : "PM";
  $h = $H % 12;
  echo "<option value='".str_pad($H, 2, "0", STR_PAD_LEFT).":00:00'> "
    .str_pad($h, 2, "0", STR_PAD_LEFT).":00 $ap</option>\n";
  echo "<option value='".str_pad($H, 2, "0", STR_PAD_LEFT).":30:00'> "
    .str_pad($h, 2, "0", STR_PAD_LEFT).":30 $ap</option>\n";
}


Not trying to initiate a 'lines war' but this is just 2 or 3 lines of code and works just fine as well.

for($hour=12; $hour < 30; $hour++)
{
    echo"<option value=\'".($hour >= 24 ? ($hour-23).":00:00": ($hour-11).":00:00")."\'>".($hour >= 24 ? ($hour-23).":00:00 AM": ($hour-11).":00:00 PM")."</option>";
    echo"<option value=\'".($hour >= 24 ? ($hour-23).":30:00": ($hour-11).":30:00")."\'>".($hour >= 24 ? ($hour-23).":30:00 AM": ($hour-11).":30:00 PM")."</option>";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消