Search code examples
phpformsinputasort

adding date array values ascending in php


This php script generates a input-field.

$html_output .= '<select name="date_year" id="year_select">'."\n";
for ($year = date("Y") - 45; 
$year <= (date("Y") - $year_limit); $year++) {
$html_output .= '<option>' . $year . '</option>'."\n";
}

However i would like to change the $year from '2014 to 1969' instead of '1969 to 2014'.

I have found asort()-command but have failed to get it to work.

$html_output .= '<option>' . asort($year) . '</option>'."\n";

Solution

  • Your code seems to be overloaded... Try this:

    $html_output .= '<select name="date_year" id="year_select">'."\n";
    $year = date("Y");
    $limit = date("Y")-100;
    for($i = $year; $i>=$limit; $i--){
    echo "<option value='$i'>$i</option>";
    }
    

    Example: http://3v4l.org/veiHi