Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWebsite Automation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Always-current Year-selection Dropdown List

A gotcha when designing dropdown lists for selecting a year is that the current year will change.

When the new year comes around, you have to remember to update the page source code to change the dropdown list.

Solve it by letting PHP generate the dropdown list options for you. PHP always knows what the current year is and can generate the list accordingly.

The PHP script that comes with this article has two places to customize:

  1. The number of consecutive years the list shall have for selection options.

  2. The last selectable year the list shall have relative to the current year.

In other words, you can specify how long your year selection list is to be. And specify what year is last on the list relative to the current year.

"Relative to the current year" can be the current year. Optionally, it can be a certain number of years in the past (if you have an age-limited application form, for instance). Or, as another option, it can be a certain number of years in the future (if you have a reservation form, for example).

Whatever your "relative to current year" setting, the dropdown list will automatically adjust at midnight of every year change.

When the PHP script is uploaded to the server and its URL noted, this code pulls the always-current year list into your dropdown.

<select>
<?php include("location_of_PHP_script")) ?>
</select>

(You put any id, name, and CSS you need into the select tag.)

To insert a blank selection at the top of the list, this can do it for you.

<select>
<option value=""></option>
<?php include("location_of_PHP_script")) ?>
</select>

The idea is that the PHP script provides the year selection options. You provide everything else.

The PHP will keep your dropdown up to date.

The PHP Script

It's a short PHP script. And it has two places to customize. Examples for various customizations are below the source code.

<?php
/*
Dropdown Options List Generator
Version 1.0
January 21, 2019
Will Bontrager Software LLC
https://www.willmaster.com/
*/

/* Two places to customize. See article for info. */

// Place 1, the number of years for the option list.
$NumberOfYearsInList = 10;

// Place 2, the last year in the list relative to the current.
//   (0=Current year. 
//    Positive number indicates number of years in the future.
//    Negative number indicates number of years in the past.
$LastYearRelativeToCurrentYear = 0;

/* End of customization. */
$LastYearOnList = date('Y') + $LastYearRelativeToCurrentYear;
$FirstYearOnList = $LastYearOnList - $NumberOfYearsInList + 1;
for( $i=$FirstYearOnList; $i<=$LastYearOnList; $i++ ) { echo "<option value='$i'>$i</option>"; }
?>

After uploading to your server, make a note of the PHP script's URL. You'll need the URL for the include() function noted in the example code further above.

Customizations.

Change the number 10 in $NumberOfYearsInList = 10; to the number of years you want on your dropdown list.

Change the number 0 in $LastYearRelativeToCurrentYear = 0; to what the last year number shall be relative to the current year number:

The number 0 — The current year number will be the last on the list.
A positive number — The last year number on the last will be in the future of the current year by the number of years indicated.
A negative number — The last year number on the last will be in the past of the current year by the number of years indicated.

Examples of Use

This first example is simply a list of 10 years with the last year on the list being the current year. These are the customizations.

$NumberOfYearsInList = 10;
$LastYearRelativeToCurrentYear = 0;

And here is the example.

The second example is a list of 20 years with the last year on the list being 16 years in the past. These are the customizations.

$NumberOfYearsInList = 20;
$LastYearRelativeToCurrentYear = -16;

Here is the example.

This last example is a list of 5 years with the last year on the list being 5 years in the future. These are the customizations.

$NumberOfYearsInList = 5;
$LastYearRelativeToCurrentYear = 5;

This is the example.

With each example, regardless what year it is that you are reading this article here at the Willmaster.com website, the dropdown will be correct relative to the current year.

Always Current

Year-selection dropdowns published with the technique provided here will always be current. When the year changes, the options list changes accordingly.

Changing dropdowns every January can now be removed from your to-do list or, if you don't use such a list, removed from your mental bulletin board.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC