Return to Snippet

Revision: 13521
at April 27, 2009 10:47 by iridium


Updated Code
// Number of years to go back
$yearRange = 100;

// Selected Age
$ageLimit = 18;

// Generate Options
$thisYear = date('Y');
$startYear = ($thisYear - $yearRange);
$selectYear = ($thisYear - $ageLimit);

foreach (range($thisYear, $startYear) as $year) {
	$selected = "";
	if($year == $selectYear) { $selected = " selected"; }
	print '<option' . $selected . '>' . $year . '</option>
';
}

Revision: 13520
at April 27, 2009 10:44 by iridium


Updated Code
// Number of years to go back
$yearRange = 100;

// Selected Age
$ageLimit = 18;

// Generate Options
$thisYear = date('Y');
$startYear = ($thisYear - $yearRange);
$limitYear = ($thisYear - $ageLimit);

foreach (range($thisYear, $startYear) as $year) {
	$selected = "";
	if($year == $limitYear) { $selected = " selected"; }
	print '<option' . $selected . '>' . $year . '</option>
';
}

Revision: 13519
at April 27, 2009 05:15 by iridium


Initial Code
// Number of years to go back
$yearRange = 100;

// Generate Options
$startYear = (date('Y') - $yearRange);
foreach (range(date('Y'), $startYear) as $year) {
	print '<option>' . $year . '</option>
';
}

Initial URL

                                

Initial Description
Generates an options list of years for a select box, going from current year backwards through the number of years specified in the first variable.

Useful for birthday fields on signup forms.

eg. 2009, 2008, 2007, 2006 ... 1912, 1911, 1910, 1909

EDITED: Now includes option to set the initially selected value to a set number of years ago. Useful if your form is targetted at a certain age group. Will default to current year if set to '0'.

Initial Title
Year List Select Box

Initial Tags
list

Initial Language
PHP