Another post from the archives.
After 2009 rolled in and all our sites failed to follow suite because “someone” hard-coded the years — but only up to 2008 — I quickly had to write something a bit more – well, smart.
I needed an HTML drop-down box with a set of years starting from a fixed year, but extending to the current year so that it would always be up-to-date and I wouldn’t have to run around updating reams of HTML each year.
I have assigned the year from which to start from as $INCEPTION_DATE
. To get the current year, which will be the top of the range (we don’t need future dates), you simply use the PHP date function like so:
. The simply iterate through the years. I have also set the drop-down box so that the current year will be selected by default.date('Y')
$INCEPTION_DATE = 2008; $current_year = date('Y'); for($c = $INCEPTION_DATE; $c <= $current_year; $c++) { $sel = ($current_year == $c) ? 'selected="selected"' : ''; echo '<option value="'.$c.'" '.$sel.'>'.$c.'</option>'; }
A little bit of forward thinking can save a whole lot of time!