以半小时为增量创建选择列表


Creating select list in half-hour increments

我有两个变量:$start_time$end_time。这些值是24小时XX:XX格式的时间,并且总是以:00:30结尾。它们应该定义一个范围来确定HTML表单中下拉选择列表中出现的时间,但表单以X:XX am/pm表示法显示格式。

例如,如果我有以下值:

$start_time = "11:00";
$end_time   = "13:30";

我用什么PHP代码来生成如下所示的选择列表?

<option value="11:00">11:00 am</option>
<option value="11:30">11:30 am</option>
<option value="12:00">12:00 pm</option>
<option value="12:30">12:30 pm</option>
<option value="13:00">1:00 pm</option>
<option value="13:30">1:30 pm</option>

我知道如何通过手动构建一个包含所有可能值的数组,然后反向工作来实现这一点,但必须有一种更优雅的方式来使用PHP的内置时间函数。

任何指导都将不胜感激。

相当简单,有一点strtotime()和date()的魔力:

考虑:

<?php
$start = "11:00";
$end = "13:30";
$tStart = strtotime($start);
$tEnd = strtotime($end);
$tNow = $tStart;
while($tNow <= $tEnd){
  echo date("H:i",$tNow)."'n";
  $tNow = strtotime('+30 minutes',$tNow);
}

在标签中正确格式化和输出内容只是一个练习,请参阅文档中的date()。

这似乎也能产生你想要的东西。。

$start=strtotime('10:00');
$end=strtotime('21:30');
for ($halfhour=$start;$halfhour<=$end;$halfhour=$halfhour+30*60) {
    printf('<option value="%s">%s</option>',date('H:i',$halfhour),date('g:i a',$halfhour));
}

我不知道你可以使用php的日期和时间函数,因为这些函数更适合处理特定的时间,但这里有一个更优雅的解决方案:

$options = array();
foreach (range(0,24) as $fullhour) {
   $parthour = $fullhour > 12 ? $fullhour - 12 : $fullhour;
   $parthour .= $fullhour > 11 ? " pm" : " am";
   $options["$fullhour:00"] = $parthour;
   $options["$fullhour:30"] = $parthour;
}

尝试创建间隔为30分钟的时间数组。我已经测试过了。

    $options = array(); $min30=array('00','30');
    foreach (range(0,23) as $fullhour) {
       $parthour = $fullhour > 12 ? $fullhour - 12 : $fullhour;
        foreach($min30 as $int){
            if($fullhour > 11){
                $options[$fullhour.".".$int]=$parthour.":".$int." PM";
            }else{
                if($fullhour == 0){$parthour='12';}
                $options[$fullhour.".".$int]=$parthour.":".$int." AM" ;
            }
        }
    }
    print_r($options);

另一种方法是使用DateTime对象使用PHP完全解决您的问题

// Make a DateTime object with the current date and time
$today = new DateTime();
// Make an empty array to contain the hours
$aHours = array();
// Make another DateTime object with the current date and time
$oStart = new DateTime('now');
// Set current time to midnight
$oStart->setTime(0, 0);
// Clone DateTime object (This is like 'copying' it)
$oEnd = clone $oStart;
// Add 1 day (24 hours)
$oEnd->add(new DateInterval("P1D"));
// Add each hour to an array
while ($oStart->getTimestamp() < $oEnd->getTimestamp()) {
    $aHours[] = $oStart->format('H');
    $oStart->add(new DateInterval("PT1H"));
}
// Create an array with halfs
$halfs = array(
    '0',
    '30',
);
// Get the current quarter
$currentHalf = $today->format('i') - ($today->format('i') % 30);

然后打印:

<select name="" id="">
    <option value="-1">Choose a time</option>
    <?php foreach ($aHours as $hour): ?>
        <?php foreach ($halfs as $half): ?>
            <option value="<?= sprintf("%02d:%02d", $hour, $half); ?>" <?= ($hour == $today->format('H') && $half == $currentHalf) ? 'selected' : ''; ?>>
                <?= sprintf("%02d:%02d", $hour, $half); ?>
            </option>
        <?php endforeach; ?>
    <?php endforeach; ?>
</select>

在上面的示例中,您将生成一个包含一天中所有时间的下拉列表。将自动选择当前小时和半小时。

根据爆炸药丸的代码,我认为这是正确的方法。

$options = array();
foreach (range(0,23) as $fullhour) 
{
    $parthour = $fullhour > 12 ? $fullhour - 12 : $fullhour;
    $sufix = $fullhour > 11 ? " pm" : " am";
    $options["$fullhour:00"] = $parthour.":00".$sufix;
    $options["$fullhour:30"] = $parthour.":30".$sufix;
}

这是一个完整的工作版本,我正在使用它(从上面修改)。我所做的更改是将选项标记的值格式化为进入sqldb。我还将00:00改为午夜,将12:00改为中午。

(再次编辑以显示选择选项)

<select name="starttime">
<?php 
foreach (range(0,23) as $fullhour) {
$fullhour2digit = strlen($fullhour)==1 ? '0' . $fullhour : $fullhour;
$parthour = $fullhour > 12 ? $fullhour - 12 : $fullhour;
$parthour .= $fullhour > 11 ? ":00 pm" : ":00 am";
$parthour = $parthour=='0:00 am' ? 'midnight' : $parthour;
$parthour = $parthour=='12:00 pm' ? 'noon' : $parthour;
$parthalf = $fullhour > 12 ? $fullhour - 12 : $fullhour;
$parthalf .= $fullhour > 11 ? ":30 pm" : ":30 am";

//SHOWS THE TEST FOR 'SELECTED' IN THE OPTION TAG
     echo '<option ';
     if (date("H:i:s", strtotime($startdate)) === $fullhour2digit . ':00:00')
        {echo ' SELECTED ';}
     echo 'value="' . $fullhour2digit . ':00:00">' .  $parthour . '</option>';
     echo '<option ';
     if (date("H:i:s", strtotime($startdate)) === $fullhour2digit  . ':30:00')
        {echo ' SELECTED ';}
     echo 'value="' . $fullhour2digit . ':30:00">' .  $parthalf . '</option>';
}
?>
</select>

此输出(截断)。。。

<select name="starttime">
<option value="00:00:00">midnight</option><option value="00:30:00">0:30 am</option>
 ....
<select>
  <?php
     $h = 0;
     $m = 0;
     while ($h != 24.5) {
       if(strpos($h,".") !== false){
           $v = ($h-0.5).'30';
       }else{
           $v = $h.'00';
       }
       if ($h < 10) {
           $v = '0'.$v;
       }
       echo '<option>'.$v.'</option>';
       $h = $h + 0.5;
     }
  ?>
</select>

以下是一个使用AM/PM:的12小时解决方案

<?php
$steps   = 15; // only edit the minutes value
$current = 0;
$loops   = 24*(60/$steps);
for ($i = 0; $i < $loops; $i++) {
    $hour = $i/(60/$steps);
    $time = sprintf('%02d:%02d %s', $hour, $current%60, intval($hour/12) == 0 ? ' AM' : ' PM');
    echo '<option>'.$time.'</option>';
    $current += $steps;
}
?>

24小时:

<?php
$steps   = 15; // only edit the minutes value
$current = 0;
$loops   = 24*(60/$steps);
for ($i = 0; $i < $loops; $i++) {
    $time = sprintf('%02d:%02d', $i/(60/$steps), $current%60);
    echo '<option>'.$time.'</option>';
    $current += $steps;
}
?>