我正在页面上创建一个下拉菜单,供人们选择会议的持续时间。在设计时,我只是硬编码了一些选项,但现在我需要创造更优雅的东西......
这就是结果需要:
<option value="15">15 minutes</option>
<option value="30">30 minutes</option>
<option value="45">45 minutes</option>
<option value="60">1 hour</option>
<option value="75">1 hour 15 minutes</option>
<option value="90">1 hour 30 minutes</option>
<option value="105">1 hour 45 minutes</option>
<option value="120">2 hours</option>
我需要以15分钟的增量继续总共24小时。
我从这样的东西开始,它产生了正确方向的东西,但就文本等格式化而言,我就是迷失方向。
for ($i = 1; $i <= 24; $i++){
for ($j = 0; $j <= 45; $j+=15){
echo $i . ':'.$j.'<br />';
}
}
用PHP处理这个问题的简单方法是什么?
答案 0 :(得分:2)
<?php
echo "<select>";
for($i = 0; $i <= 23; $i++){
for ($j = 0; $j <= 45; $j += 15){
if ($i === 0 && $j === 0){
//do nothing
}
else{
//get string for hours
switch($i){
case 0:
$hours = "";
break;
case 1:
$hours = "1 hour";
break;
default:
$hours = $i . " hours";
break;
}
//get string for minutes
switch($j){
case 0:
$minutes = "";
break;
default:
$minutes = $j . " minutes";
break;
}
$value = ($hours * 60) + $minutes;
//output
echo "<option value='" . $value . "'>" . $hours . " " . $minutes . "</option>";
}
}
}
echo "</select>";
答案 1 :(得分:-1)
我是这样做的:
for ($min_total=15; $min_total<=1440; $min_total += 15) {
$hours = floor($min_total / 60);
$min = $min_total % 60;
$text = '';
if ($hours > 0) {
$text .= $hours;
$text .= ' hour';
if ($hours != 1) {
$text .= 's';
}
$text .= ' ';
}
if ($min > 0) {
$text .= $min . ' minutes';
}
echo "<option value='" . $min_total . "'>" . trim($text) . "</option>\n";
}
结果:
<option value='15'>15 minutes</option>
<option value='30'>30 minutes</option>
<option value='45'>45 minutes</option>
<option value='60'>1 hour</option>
<option value='75'>1 hour 15 minutes</option>
<option value='90'>1 hour 30 minutes</option>
<option value='105'>1 hour 45 minutes</option>
<option value='120'>2 hours</option>
<option value='135'>2 hours 15 minutes</option>
<option value='150'>2 hours 30 minutes</option>
<option value='165'>2 hours 45 minutes</option>
<option value='180'>3 hours</option>
<option value='195'>3 hours 15 minutes</option>
<option value='210'>3 hours 30 minutes</option>
<option value='225'>3 hours 45 minutes</option>
<option value='240'>4 hours</option>
<option value='255'>4 hours 15 minutes</option>
<option value='270'>4 hours 30 minutes</option>
<option value='285'>4 hours 45 minutes</option>
<option value='300'>5 hours</option>
<option value='315'>5 hours 15 minutes</option>
<option value='330'>5 hours 30 minutes</option>
<option value='345'>5 hours 45 minutes</option>
<option value='360'>6 hours</option>
<option value='375'>6 hours 15 minutes</option>
<option value='390'>6 hours 30 minutes</option>
<option value='405'>6 hours 45 minutes</option>
<option value='420'>7 hours</option>
<option value='435'>7 hours 15 minutes</option>
<option value='450'>7 hours 30 minutes</option>
<option value='465'>7 hours 45 minutes</option>
<option value='480'>8 hours</option>
<option value='495'>8 hours 15 minutes</option>
<option value='510'>8 hours 30 minutes</option>
<option value='525'>8 hours 45 minutes</option>
<option value='540'>9 hours</option>
<option value='555'>9 hours 15 minutes</option>
<option value='570'>9 hours 30 minutes</option>
<option value='585'>9 hours 45 minutes</option>
<option value='600'>10 hours</option>
<option value='615'>10 hours 15 minutes</option>
<option value='630'>10 hours 30 minutes</option>
<option value='645'>10 hours 45 minutes</option>
<option value='660'>11 hours</option>
<option value='675'>11 hours 15 minutes</option>
<option value='690'>11 hours 30 minutes</option>
<option value='705'>11 hours 45 minutes</option>
<option value='720'>12 hours</option>
<option value='735'>12 hours 15 minutes</option>
<option value='750'>12 hours 30 minutes</option>
<option value='765'>12 hours 45 minutes</option>
<option value='780'>13 hours</option>
<option value='795'>13 hours 15 minutes</option>
<option value='810'>13 hours 30 minutes</option>
<option value='825'>13 hours 45 minutes</option>
<option value='840'>14 hours</option>
<option value='855'>14 hours 15 minutes</option>
<option value='870'>14 hours 30 minutes</option>
<option value='885'>14 hours 45 minutes</option>
<option value='900'>15 hours</option>
<option value='915'>15 hours 15 minutes</option>
<option value='930'>15 hours 30 minutes</option>
<option value='945'>15 hours 45 minutes</option>
<option value='960'>16 hours</option>
<option value='975'>16 hours 15 minutes</option>
<option value='990'>16 hours 30 minutes</option>
<option value='1005'>16 hours 45 minutes</option>
<option value='1020'>17 hours</option>
<option value='1035'>17 hours 15 minutes</option>
<option value='1050'>17 hours 30 minutes</option>
<option value='1065'>17 hours 45 minutes</option>
<option value='1080'>18 hours</option>
<option value='1095'>18 hours 15 minutes</option>
<option value='1110'>18 hours 30 minutes</option>
<option value='1125'>18 hours 45 minutes</option>
<option value='1140'>19 hours</option>
<option value='1155'>19 hours 15 minutes</option>
<option value='1170'>19 hours 30 minutes</option>
<option value='1185'>19 hours 45 minutes</option>
<option value='1200'>20 hours</option>
<option value='1215'>20 hours 15 minutes</option>
<option value='1230'>20 hours 30 minutes</option>
<option value='1245'>20 hours 45 minutes</option>
<option value='1260'>21 hours</option>
<option value='1275'>21 hours 15 minutes</option>
<option value='1290'>21 hours 30 minutes</option>
<option value='1305'>21 hours 45 minutes</option>
<option value='1320'>22 hours</option>
<option value='1335'>22 hours 15 minutes</option>
<option value='1350'>22 hours 30 minutes</option>
<option value='1365'>22 hours 45 minutes</option>
<option value='1380'>23 hours</option>
<option value='1395'>23 hours 15 minutes</option>
<option value='1410'>23 hours 30 minutes</option>
<option value='1425'>23 hours 45 minutes</option>
<option value='1440'>24 hours</option>