我想知道如何在php中循环一个半小时的时间我想要输出如下:
<select style='width:250px;' name='days' onchange='return timeSchedvalue(this.value)'>
<option value='6:00-6:30 am'>6:00-7:30 am</option>
<option value='6:30-7:00 am'>6:30-7:00 am</option>
<option value='7:30-8:00 am'>7:30-8:00 am</option>
<option value='8:00-8:30 am'>8:00-8:30 am</option>
<option value='8:30-9:00 am'>8:30-9:00 am</option>
<option value='9:00-9:30 am'>9:00-9:30 am</option>
<option value='9:30-10:00 am'>9:30-10:00 am</option>
<option value='10:00-10:30 am'>10:00-10:30 am</option>
<option value='10:30-11:00 am'>10:30-11:00 am</option>
<option value='11:00-11:30 am'>11:00-11:30 am</option>
<option value='11:30-12:00 am'>11:30-12:00 am</option>
<option value='12:00-12:30 pm'>12:00-12:30 pm</option>
</select>
循环将于上午6:00至6:30开始,并将于晚上10:00至晚上10:30结束。
谢谢!
帮助非常感谢。
答案 0 :(得分:7)
$time = mktime(0, 0, 0, 1, 1);
for ($i = 0; $i < 86400; $i += 1800) { // 1800 = half hour, 86400 = one day
printf('<option value="%1$s-%2$s">%1$s-%2$s</option>',
date('g:i', $time + $i), date('g:i a', $time + $i + 1800));
}
根据需要摆弄起始时间和结束条件。
答案 1 :(得分:2)
<select style='width:250px;' name='days' onchange='return timeSchedvalue(this.value)'>
<?php
$time = '6:00'; // start
for ($i = 0; $i <= 12; $i++)
{
$prev = date('g:i a', strtotime($time)); // format the start time
$next = strtotime('+30mins', strtotime($time)); // add 30 mins
$time = date('g:i a', $next); // format the next time
echo "<option value=\"$prev - $time\">$prev - $time</option>";
}
?>
</select>
答案 2 :(得分:2)
如果你愿意,可以使用它。
for ($x = 6,$x1 = 1; $x < 22.5; $x+=0.5,$x1++)
{
$gen_date=date('h:i A', mktime(0, 0, 0) + (60 * $x * 60));
$temp=$x+0.5;
$gen_date1=date('h:i A', mktime(0, 0, 0) + (60 * $temp * 60));
echo $x1.'='.$gen_date.'-'.$gen_date1.'<br>';
}
使用$ x1作为索引。 根据您的需要进行编辑
答案 3 :(得分:1)
嗯,最简单的方法是保留所有间隔的数组并循环遍历它,也许就像:
array ('06:00', '06:30', '07:00', '07:30'... );
或者可能像:
array ('06:00 - 06:30', '06:30 - 07:00', '07:00 - 07:30'...);
答案 4 :(得分:1)
在您的情况下,您需要创建一个上午6:00的开始时间 - &gt; mktime(6,0,0,0,0,0)
然后你需要每次下载30分钟......
这可以在for循环中轻松完成:
<select style='width:250px;' name='days' onchange='return timeSchedvalue(this.value)'>
<?php
for ($i = 0; $i <= 960; $i+=30) {
$time1 = date('h:i a', mktime(6, $i, 0, 0, 0, 0));
$time2 = date('h:i a', mktime(6, $i+30, 0, 0, 0, 0));
echo "<option value='" . $time1 . " - " . $time2 . "'>" .$time1 . " - " . $time2 . "</option>";
}
?>
</select>
答案 5 :(得分:0)
这样的事情应该这样做。您基本上是在一段时间内添加30分钟的块并循环直到您获得正确数量的选项。
//get the timestamp for 6.30 am
$time = strtotime('6.30 am'); //not important what the date is.
//start adding 30mins
$times = array();
$halfHour = 60 * 30;
$blocks = 12; //adjust this to get the number of options
while ($blocks) {
$times[] = date('h:i', $time) . ' - ' . date('h:i a', ($time += $halfHour)); //I keep incrementing the time by half an hour
$blocks--;
}
var_dump($times);
答案 6 :(得分:0)
这简单地遍历我们的每一半,然后在每个上面调用日期。
$inc = 30 * 60;
$start = (strtotime('6AM')); // 6 AM
$end = (strtotime('10PM')); // 10 PM
echo "<select>";
for( $i = $start; $i <= $end; $i += $inc )
{
// to the standart format
$range = date( 'g:i', $i ) . ' - ' .
// $inc = 30 minutes so this will be .5 hr later
date( 'g:i A', $i + $inc );
echo "<option value=\"$range\">$range</option>" . PHP_EOL;
}
echo "</select>";
答案 7 :(得分:0)
<?php
$timeFormat = "%02d:%02d";
$startHour = 6;
$endHour = 12;
$timeInterval = 30; // in minutes
$currentHour = $startHour;
$currentMinute = 0;
while ($currentHour <= $endHour) {
$timeRange = sprintf($timeFormat, $currentHour, $currentMinute) . ($currentHour < 12 ? " am" : " pm");
$currentMinute = $currentMinute + $timeInterval;
if ($currentMinute > 59) {
$currentHour++;
$currentMinute = $currentMinute - 60;
}
$timeRange .= " - " . sprintf($timeFormat, $currentHour, $currentMinute) . ($currentHour < 12 ? " am" : " pm");
if ($currentHour <= $endHour) {
echo "## " . $timeRange . "\n";
}
}
?>