I would like to fill a select box with year starting from 1950 to the current year. How can I achieve this using PHP? I would not like to use JavaScript for this.
<select><?php
$currentYear = date('Y');
foreach (range(1950, $currentYear) as $value) {
echo "< option>" . $value . "</option > ";
}
?>
</select>
Use
rangeto create an array containing all the required years, loop that array and print anoptionfor each of the values.You can use
date('Y')to figure out the current year.Try it here: http://codepad.viper-7.com/Pw3U4O
Documentation
foreach– http://php.net/manual/en/control-structures.foreach.phprange– http://php.net/manual/en/function.range.phpdate– http://php.net/manual/en/function.date.php