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:
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments