doutuo2829 2017-11-07 20:45
浏览 59
已采纳

我有一个时间列表,我想放入下拉菜单

Kind of new to this, so sorry if I'm asking a dumb question.

I have made a list of times, which I have to put into a select menu. Kinda like this .

I actually want my list to be from 16:00 until 01:45 or 02:00 instead of 00:00-01:45 & 16:00-23:45, but I don't know how. And then the second thing is I couldn't manage to get this list into a

<select>

dropdown menu

This is the code I've got so far:

<?php 

$exclude = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);

function echo_datelist($i, $j)
{
    $time = str_pad($i, 2, '0', STR_PAD_LEFT).':'.str_pad($j, 2, '0', STR_PAD_LEFT);            
    echo $time.'<br />';
}

for ($i = 00; $i <= 23; $i++)
{ 
    for ($j = 0; $j <= 45; $j+=15)
    { 
        if (in_array($i, $exclude)) continue;
        echo_datelist($i, $j); 
    }
}
?>
  • 写回答

2条回答 默认 最新

  • dongnvwang8591 2017-11-07 21:03
    关注

    If you want to start at 16:00 and go to 1:45 you could use a modulus like so inside your first for loop $t = ($i + 16) % 24 and then use this variable instead of $i inside you other loop. If you want to put it in a select element you need to surround your loop with <select> </select> here is an example of what you are looking for.

    <?php
    $exclude = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
    
    function echo_datelist($i, $j)
    {
        $time = str_pad($i, 2, '0', STR_PAD_LEFT).':'.str_pad($j, 2, '0', STR_PAD_LEFT);            
        echo "<option value=" .$time. ">" . $time . "</option>"; // print in option
    }
    echo "<select>"; // this to enclose it all in a select element
    for ($i = 00; $i <= 23; $i++){
        $hour = ($i + 16) % 24; // this to start at 16:00 and wrap around to 1:45
        for ($j = 0; $j <= 45; $j+=15)
        {  
            if (in_array($hour, $exclude)) continue;
            echo_datelist($hour, $j); 
        }
    }
    echo "</select>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序