0

我有一个带有截止日期和时间选择选项的 PHP 页面。我想做的是将时间选择下拉列表中的时间限制为仅显示可用的工作时间,而不是从 00:00 到 24:00 的 24 小时时间列表。这是当前显示的代码,但这超出了我的想象,超出了我的基本编辑能力。任何帮助或建议将不胜感激。提前致谢,

<tr>
    <td align="left" valign="top">Due Date:</td>
    <td>
        <i>Time is based on your time zone (GM <?=$thisuser->getTZoffset()?>)</i>&nbsp;<font class="error">&nbsp;<?=$errors['time']?></font><br>
        <input id="duedate" name="duedate" value="<?=Format::htmlchars($info['duedate'])?>"
            onclick="event.cancelBubble=true;calendar(this);" autocomplete=OFF>
        <a href="#" onclick="event.cancelBubble=true;calendar(getObj('duedate')); return false;"><img src='images/cal.png'border=0 alt=""></a>
        &nbsp;&nbsp;
        <?php
         $min=$hr=null;
         if($info['time'])
            list($hr,$min)=explode(':',$info['time']);
            echo Misc::timeDropdown($hr,$min,'time');
        ?>
        &nbsp;<font class="error">&nbsp;<?=$errors['duedate']?></font>
    </td>
</tr>
4

1 回答 1

0

试试这个代码:

最佳代码:

function timeRange($start, $end, $interval="30 mins" , $outputFormat = null )
{
    $range = array();
    $start = strtotime($start); 
    $end   = strtotime($end);

    $current    = time();
    $interval   = strtotime('+'.$interval, $current);
    $diff       = $interval-$current;


    while ($start <= $end)
    {
        if( is_null($outputFormat) )
        {
            $range[] = $start;
        }else{
            $range[] = date( $outputFormat , $start);
        }
        $start += $diff;
    }
    return $range;
}
$times = timeRange('8:30', '17:30', '30 mins' , "H:i");

输出:

Array
(
    [0] => 08:30
    [1] => 09:00
    [2] => 09:30
    [3] => 10:00
    [4] => 10:30
    [5] => 11:00
    [6] => 11:30
    [7] => 12:00
    [8] => 12:30
    [9] => 13:00
    [10] => 13:30
    [11] => 14:00
    [12] => 14:30
    [13] => 15:00
    [14] => 15:30
    [15] => 16:00
    [16] => 16:30
    [17] => 17:00
    [18] => 17:30
)

简单代码:

function timeInterval( $start , $end , array $interval = array() )
{
    $list = array();
    for( $i = $start; $i <= $end ;$i++ )
    {
        foreach( $interval AS $min )
        {
            $list[] = sprintf("%02d",$i). ":" . $min;
        }
    }
    return $list;   
}


highlight_string( print_r( timeInterval( 8 , 17 , array( "00" , "30" ) ), true ) );

输出:

Array
(
    [0] => 08:00
    [1] => 08:30
    [2] => 09:00
    [3] => 09:30
    [4] => 10:00
    [5] => 10:30
    [6] => 11:00
    [7] => 11:30
    [8] => 12:00
    [9] => 12:30
    [10] => 13:00
    [11] => 13:30
    [12] => 14:00
    [13] => 14:30
    [14] => 15:00
    [15] => 15:30
    [16] => 16:00
    [17] => 16:30
    [18] => 17:00
    [19] => 17:30
)

还有你的 HTML:

<tr>
    <td align="left" valign="top">Due Date:</td>
    <td>
        <i>Time is based on your time zone (GM <?php echo $thisuser->getTZoffset();?>)</i>
        &nbsp;
        <font class="error">
            &nbsp;<?php echo $errors["time"];?>
        </font>
        <br/>
        <input id="duedate" name="duedate" value="<?php echo Format::htmlchars($info["duedate"]);?>" onclick="event.cancelBubble=true;calendar(this);" autocomplete="OFF" />
        <a href="#" onclick="event.cancelBubble=true;calendar(getObj('duedate')); return false;">
            <img src='images/cal.png'border=0 alt="">
        </a>
        &nbsp;&nbsp;
        <select name="time">
            <option value="">Select you time...</option>
            <?php
            foreach( timeRange('9:30', '17:30', '30 mins' , "H:i") AS $time )
            {
                echo "<option value=\"$time\">$time</option>";
            }
            ?>
        </select>
        &nbsp;<font class="error">&nbsp;<?php echo $errors["duedate"];?></font>
    </td>
</tr>

再见!

于 2013-10-18T17:07:36.267 回答