2012-10-17 69 views
0

我有這個長的代碼來顯示今天的日期&時間在表格下拉列表中。它確實工作正常,但我想知道是否有更簡單的方法來做到這一點...?PHP在表格下拉列表中顯示今天的日期/時間

我想顯示所有的月份,日期,年份(只有幾年),所有小時(24小時格式),所有分鐘和所有秒,但在每個下拉列表中選擇當前日期/時間。

<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> 

- 更新 - 這裏的清潔版本 - 謝謝!

<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> 
+0

這裏有這麼多的代碼味,我的狗已經在運行。 – moonwave99

回答

1

一些小的優化,以減少LOC計數:

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

和類似的其他下拉菜單。

1

看看你是如何調用該函數date()

$date_m = date(m); 

在你的代碼一直傳遞一個常數如mdY。這是有效的,因爲在這種情況下PHP非常寬鬆,它尋找常量,沒有找到它,然後假定你的意思是字符串m並將其傳遞給函數。

雖然代碼工作,你還是應該通過圍繞價值加上引號解決這個問題傳遞給date()如:

$date_m = date('m'); 

這樣做的原因是爲了防止未來的任何錯誤,你可以定義一個常數叫做m。例如:

define('m', 'something...'); 

// here you would be passing something... to the date function and not m 
$date_m = date(m); 

在簡化代碼,然後@ MarcB的建議而言將是一件好事。另一個選擇是創建一個函數,該函數將數組和選定選項作爲參數,並生成HTML。這將消除代碼中的大量HTML重複,因爲不必一遍又一遍地重複下拉HTML,您可以創建一個數組並僅調用您的函數,例如

echo getHTMLDropdown($options, $selectedOption);