Make a dynamic year dropdown using PHP

Ever wanted to have a dropdown that automatically showed the current year and the few years before it?

This is a quick and easy way to do exactly that!

<select name="year">
    <?
    for($i=date("Y")-5;$i<=date("Y");$i++) {
        $sel = ($i == date('Y')) ? 'selected' : '';
        echo "<option value=".$i." ".$sel.">".date("Y", mktime(0,0,0,0,1,$i))."</option>";
    }
    ?>
</select>
Tags:

Leave a Reply

Your email address will not be published.