douganmo1121 2012-10-17 16:25
浏览 160
已采纳

PHP在表单下拉列表中显示今天的日期/时间

i have this long code to show today's date & time in a form drop down lists. It does work fine but I was wondering if there's a simpler way to do it...?

I want to show all months, days, years (only a couple of years), all hours (24 hour format), all minutes and all seconds but have the current date/time selected in each drop down list.

<select name="event_month">
<?
$arr_m = array("January","February","March","April","May","June","July","August","September","October","November","December");
$date_m = date(m);
$count = 1;
$months = 12;
do{
    $month = $arr_m[$count-1];
    if ($date_m == $count) {
        echo ("<option value='$month' selected='selected'>$month</option>");
        } else {
        echo ("<option value='$month'>$month</option>");
    }
    $count++;
}while($count<=$months);
?></select>

<select name="start-day">
<?
$date_d = date(d);
$day=1;
$days = 31;
do{
    if($date_d == $day){
        echo ("<option value='$day' selected='selected'>$day</option>");
    }else{
        echo ("<option value='$day'>$day</option>");
    }
    $day++;
}while($day<=$days);
?></select>

<select name="start-year"><?
$date_y = date(Y);
$year =intval($date_y);
$years = $year+2;
do{
    if($date_y == $year){
        echo ("<option value='$year' selected='selected'>$year</option>");
    }else{
        echo ("<option value='$year'>$year</option>");
    }
    $year++;
}while($year<=$years);
?></select>

<select name="start-hour"><?
$date_h = date(H);
$hour = 1;
$hours = 24;
do{
    if($date_h == $hour){
        echo ("<option value='$hour' selected='selected'>$hour</option>");
    }else{
        echo ("<option value='$hour'>$hour</option>");
    }
    $hour++;
}while($hour<=$hours);
?></select>

<select name="start-min"><?
$date_min = date(i);
$min = 1;
$mins = 60;
do{
    if($date_min == $min){
        echo ("<option value='$min' selected='selected'>$min</option>");
    }else{
        echo ("<option value='$min'>$min</option>");
    }
    $min++;
}while($min<=$mins);
?></select>

<select name="start-sec"><?
$date_sec = date(s);
$sec = 1;
$secs = 60;
do{
    if($date_sec == $sec){
        echo ("<option value='$sec' selected='selected'>$sec</option>");
    }else{
        echo ("<option value='$sec'>$sec</option>");
    }
    $sec++;
}while($sec<=$secs);
?></select>

-- UPDATE -- here's the cleaner version - thanks!

<select name="start-month">
<?
$arr_m = array("January","February","March","April","May","June","July","August","September","October","November","December");
$month = date('m');
for ($i = 0; $i <= 12; $i++) {
    $name = $arr_m[$i-1];
    $sel = ($i == $month) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$name</option>";
}
?>
</select>


<select name="start-day">
<?
$day = date('d');
for ($i = 0; $i <= 31; $i++) {
    $sel = ($i == $day) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>

<select name="start-year">
<?
$year = date('Y');
$years = $year+2;
for ($i = $year; $i <= $years; $i++) {
    $sel = ($i == $year) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>

<select name="start-hour">
<?
$hour = date('G');
for ($i = 0; $i <= 24; $i++) {
    $sel = ($i == $hour) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>

<select name="start-min">
<?
$min = date('i');
for ($i = 0; $i <= 59; $i++) {
    $sel = ($i == $min) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>

<select name="start-sec">
<?
$sec = date('s');
for ($i = 0; $i <= 59; $i++) {
    $sel = ($i == $sec) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>
  • 写回答

2条回答 默认 最新

  • duanke2503 2012-10-17 16:27
    关注

    A few minor optimizations, to reduce the LOC count:

    for ($i = 0; $i <= 59; $i++) {
        $sel = ($i == $sec) ? ' selected="selected"' : '';
        echo "<option value=\"$i\"$sel>$i</option>";
    }
    

    and similarly for the other dropdowns.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型
  • ¥15 关于腾讯云服务器中,PHP页面无法打开的问题