PHP Dropdown populating years - how to select curr

2020-07-14 06:03发布

I have this PHP Code that is populating values in a SELECT box for the previous 10 years and the next 10 years.

<select name="fromYear"';
   $starting_year  =date('Y', strtotime('-10 year'));
   $ending_year = date('Y', strtotime('+10 year'));

    for($starting_year; $starting_year <= $ending_year; $starting_year++) {
 echo '<option value="'.$starting_year.'">'.$starting_year.'</option>';
  }             
 echo '<select>

how can I make it automatically select the current year?

标签: php
8条回答
家丑人穷心不美
2楼-- · 2020-07-14 06:54
for($starting_year; $starting_year <= $ending_year; $starting_year++) { 
 echo '<option value="'.$starting_year.'"'. if(starting_year == date('Y')) echo "selected"  .'>'.$starting_year.'</option>';   

} 
查看更多
看我几分像从前
3楼-- · 2020-07-14 07:00
<select name="fromYear"';
 $starting_year  =date('Y', strtotime('-10 year'));
 $ending_year = date('Y', strtotime('+10 year'));
 $current_year = date('Y');
 for($starting_year; $starting_year <= $ending_year; $starting_year++) {
     echo '<option value="'.$starting_year.'"';
     if( $starting_year ==  $current_year ) {
            echo ' selected="selected"';
     }
     echo ' >'.$starting_year.'</option>';
 }               
 echo '<select>';
查看更多
登录 后发表回答