Date Drop Down List

This simple PHP snippet shows how to show a drop down list of dates shown in descending order with current year selected. The data range is easily configured as is the pre-selected date.

The code is valid XHTML and can be modified to pre-select any date within the range or none at all if desired..


<?php
error_reporting
(E_ALL);
    
$startdate = ( date('Y') - 25 );
$current   date('Y'); 
$maxdate   = ( date('Y') + 5  );
    
    
$validfrom '<select name="validfrom">'."\n";
    
foreach ( 
range($maxdate,$startdate) as $years ){
$selected = ($years == $current ) ? ' selected="selected"' '';
    
$validfrom .= '<option value="'$years .'"'$selected .'>'$years .'</option>'."\n";
}
    
$validfrom .= '</select>';
    
echo 
$validfrom;

Syndicate content