Menu

PHP dynamic year selector

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: date('Y'). The simply iterate through the years. I have also set the drop-down box so that the current year will be selected by default.