PHP年度下拉列表 - 显示未来20年

时间:2015-10-07 12:03:29

标签: php

我有一个PHP函数,可以给我一年的下拉列表显示一系列年份(1950年到当年减18)。我想将其修改为仅显示当前日期的下一个20年:

            <?php
//limit by age requirement
echo date_dropdown(18);
?>
<?php
function date_dropdown($year_limit = 0){
    $html_output = '    <div id="dob_select" >';
    $html_output .= '        <label>Renewal Date:</label>';

        /*days*/
        $html_output .= '           <select class="form-control float-control" name="fp_dob_day"><option value="">Day</option>';
            for ($day = 1; $day <= 31; $day++) {
                $html_output .= '               <option value="' . $day . '">' . $day . '</option>';
            }
        $html_output .= '           </select>';

        /*months*/
        $html_output .= '           <select class="form-control float-control" name="fp_renewal_date_month"><option value="">Month</option>';
        $months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
            for ($month = 1; $month <= 12; $month++) {
                $html_output .= '               <option value="' . $month . '">' . $months[$month] . '</option>';
            }
        $html_output .= '           </select>';

        /*years*/
        $html_output .= '           <select class="form-control float-control" name="fp_renewal_date_year"><option value="">Year</option>';
            for ($year = 1950; $year <= (date("Y") - $year_limit); $year++) {
                $html_output .= '               <option value="' . $year . '">' . $year . '</option>';
            }
        $html_output .= '           </select>';

        $html_output .= '   </div>';
    return $html_output;
}
?>

我在接下来的20年中获得了最好的输出,但不在正确的HTML选择元素中 - 它们出现在其他表单元素之前。如何让它们出现在正确的位置?:

    <?php
//limit by age requirement
echo date_dropdown();
?>
<?php
function date_dropdown(){
    $current_year = date("Y");
    $range = range($current_year, ($current_year + 20));
    $html_output = '    <div id="dob_select" >';
    $html_output .= '        <label>Renewal Date:</label>';

        /*days*/
        $html_output .= '           <select class="form-control float-control" name="fp_dob_day"><option value="">Day</option>';
            for ($day = 1; $day <= 31; $day++) {
                $html_output .= '               <option value="' . $day . '">' . $day . '</option>';
            }
        $html_output .= '           </select>';

        /*months*/
        $html_output .= '           <select class="form-control float-control" name="fp_renewal_date_month"><option value="">Month</option>';
        $months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
            for ($month = 1; $month <= 12; $month++) {
                $html_output .= '               <option value="' . $month . '">' . $months[$month] . '</option>';
            }
        $html_output .= '           </select>';

        /*years*/
        $html_output .= '           <select class="form-control float-control" name="fp_renewal_date_year"><option value="">Year</option>';
            foreach($range as $r)
            {
               echo '<option value="'.$r.'">'.$r.'</option>';
            }
        $html_output .= '           </select>';
        $html_output .= '   </div>';
    return $html_output;
}
?>

2 个答案:

答案 0 :(得分:1)

不是很难。修改你的循环,我们添加今年+ 20的条件

$this_year = date("Y"); // Run this only once
for ($year = $this_year; $year <= $this_year + 20; $year++) {
    $html_output .= '               <option value="' . $year . '">' . $year . '</option>';
}

答案 1 :(得分:0)

生成显示年份的方式没有任何问题,但是,如果您注意到所有其他输出都被缓存在名为$html_output的变量中,但您直接echo&#39; d你的新年输出,所以它将直接发送到浏览器。

所以将新代码更改为

foreach($range as $r) {
    $html_output .= '<option value="'.$r.'">'.$r.'</option>';
}

并且一切都应该很好