October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Auto-Select the Current Year Range in a PHP Dropdown

Generate year-pair options in PHP and select the range ending in the current year, with clear timezone and loop-bound choices.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To select the range from the previous year through the current year, build that two-year value and compare it with each option. For example, when the current year is 2024, the selected option should be 2023-2024. Use one explicit timezone and one captured current date so the options and selection agree.

Generate and select the year range

This example lists calendar-year ranges starting at 2020 and ending in the current year. The final option is therefore the previous-year/current-year pair.

<?php
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
$currentYear = (int) $now->format('Y');
$firstYear = 2020;
$selectedRange = ($currentYear - 1) . '-' . $currentYear;
?>

<select name="year_range">
<?php for ($year = $firstYear; $year < $currentYear; $year++):
    $range = $year . '-' . ($year + 1);
    $selected = ($range === $selectedRange) ? ' selected' : '';
?>
    <option value="<?= htmlspecialchars($range, ENT_QUOTES, 'UTF-8') ?>"<?= $selected ?>>
        <?= htmlspecialchars($range, ENT_QUOTES, 'UTF-8') ?>
    </option>
<?php endfor; ?>
</select>

With a current year of 2024, the loop produces ranges from 2020-2021 through 2023-2024, and marks 2023-2024 as selected. The strict comparison works because both values use the same year-year format.

Choose the timezone that defines “current year”

The example uses UTC as an explicit choice, not a requirement for every application. PHP’s date() uses the default timezone when no timestamp is supplied; see the PHP date() manual. For timezone-aware date handling, PHP recommends using DateTimeImmutable with a timezone attached; the date and time documentation notes that “Unix timestamps do not handle timezones.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Replace UTC with the timezone your application uses, for example America/New_York. Deriving the current year once from a single DateTimeImmutable value also avoids calculating the range and selected value from separate reads that could fall on opposite sides of a year boundary.

Set the range boundaries to match the intended options

  • Only ranges ending in the current year: Use $year < $currentYear as in the example.
  • Include a range ending next year: Change the loop condition to $year <= $currentYear. That adds a future-ending option such as 2024-2025 when the current year is 2024; it does not change the selected previous/current pair.
  • Use a different starting year: Set $firstYear to the earliest starting year you want listed.

The SitePoint question’s clarification specifies a previous-year/current-year selection, but does not establish whether the options should represent calendar years, school years, or fiscal years. If the period starts on a date other than January 1, define how the application determines its period before calculating the selected option.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why a two-value range is not enough

range(date("Y") - 1, date("Y")) returns the two year numbers. It does not by itself create combined labels such as 2023-2024 or select the matching dropdown option. Build the pair for each option, then compare it with the selected pair, as the code above does. The original question and clarification are in the SitePoint Forums thread.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.