How can the date function in PHP be utilized to ensure that dropdown menus display the current date automatically?

To ensure that dropdown menus display the current date automatically, you can use the PHP date function to retrieve the current date and then populate the dropdown menu options with the current date.

<select name="day">
    <?php
    for ($i = 1; $i <= 31; $i++) {
        $selected = ($i == date('d')) ? 'selected' : '';
        echo "<option value='$i' $selected>$i</option>";
    }
    ?>
</select>

<select name="month">
    <?php
    for ($i = 1; $i <= 12; $i++) {
        $selected = ($i == date('m')) ? 'selected' : '';
        echo "<option value='$i' $selected>$i</option>";
    }
    ?>
</select>

<select name="year">
    <?php
    $currentYear = date('Y');
    for ($i = $currentYear; $i <= $currentYear + 10; $i++) {
        $selected = ($i == date('Y')) ? 'selected' : '';
        echo "<option value='$i' $selected>$i</option>";
    }
    ?>
</select>