下拉列表不适用于日,月和年


dropdown is not working with day , month and year

<?php   
        $month = array();
        for ( $i=1; $i<13; $i++ ) {
        $month = date('m', mktime(0,0,0,$i,2,2000));
        $sel = ( $i == date('n') ? ' selected="selected"' : '');
        $options1[] = "<option  value='"{$month}'" {$sel}>{$month}</option>";
}
        $options_list1 = join("", $options1);
        echo "<select name='"month'" >{$options_list1}</select>";
        for ( $j=1; $j<32; $j++ ) {
        $theday = date('d', mktime(0,0,0,0,$j,2000));
        $sel = ( $j == date('d') ? ' selected="selected"' : '');
        $options2[] = "<option  value='"{$theday}'" {$sel}>{$theday}</option>";
}
        $options_list2 = join("'r'n", $options2);
        echo "<select name='"day'" >{$options_list2}</select>";
        for ( $k=1960; $k<2016; $k++ ) {
        $theyear = date('Y', mktime(0,0,0,0,2,$k));
        $sel1 = ( $k == date('Y') ? ' selected="selected"' : '');
        $options3[] = "<option  value='"{$theyear}'" {$sel1}>{$theyear}</option>";
}
        $options_list3 = join("'r'n", $options3);
        echo "<select name='"day'" >{$options_list3}</select>";
?>

这是日,月和年的下拉列表。日和月工作正常但年份不行,在年份中我的循环无法正常工作,今天年份是 2013 年,它正在选择 2012 年。任何人都可以在这方面帮助我吗?

你得到的是前一年,因为你没有将正确的参数传递给 mktime。正如 month 参数的文档状态:

相对于上年末的月份数。 值 1 到 12 表示一年中的正常日历月 问题。小于 1 的值(包括负值)引用 上一年的月份按相反顺序排列,因此 0 是 12 月,-1 是 11月等大于 12 的值引用相应的月份 在第二年。

所以改变:

$theyear = date('Y', mktime(0,0,0,0,2,$k));

$theyear = date('Y', mktime(0,0,0,1,2,$k));

你没事。

代码板示例

相关文章: