试试这个代码:
最佳代码:
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>
<font class="error">
<?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>
<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>
<font class="error"> <?php echo $errors["duedate"];?></font>
</td>
</tr>
再见!