How can PHP dynamically determine the correct year range to display in a combobox based on a given date?
To dynamically determine the correct year range to display in a combobox based on a given date, you can use PHP's date function to get the current year and then calculate a range of years to display. Subtracting and adding a certain number of years to the current year will give you the start and end years for the range. This range can then be used to populate the combobox with the appropriate years.
$currentYear = date('Y');
$startYear = $currentYear - 50; // Adjust the number of years as needed
$endYear = $currentYear + 50; // Adjust the number of years as needed
echo '<select>';
for ($year = $startYear; $year <= $endYear; $year++) {
echo '<option value="' . $year . '">' . $year . '</option>';
}
echo '</select>';