<select name="year"><?=options(date('Y'),1900)?></select>
<select name="month"><?php echo date('m');?><?=options(1,12,'callback_month')?></select>
<select name="day"><?php echo date('d');?><?=options(1,31)?></select>
function options($from,$to,$callback=false)
{
$reverse=false;
if($from>$to)
{
$tmp=$from;
$from=$to;
$to=$tmp;
$reverse=true;
}
$return_string=array();
for($i=$from;$i<=$to;$i++)
{
$return_string[]='<option value="'.$i.'">'.($callback?$callback($i):$i).'</option>';
}
if($reverse)
{
$return_string=array_reverse($return_string);
}
return join('',$return_string);
}
function callback_month($month)
{
return date('m',mktime(0,0,0,$month,1));
}
我應該做出什麼來得到當前的月份和日期在相應的下拉列表中的初始值,因爲它是這樣與幾年(2013)。
可以在另一個參數傳遞給選項()函數,這是本年度/當月。在將下拉選項連接起來的函數中,檢查循環中的當前值是否與paased參數相同,在選項 – rakeshjain
中添加selected ='selected'您不應該調用數組'$ return_string'。爲了便於閱讀,請將其稱爲'$ options_array'或其他內容。 –