What are the best practices for using for loops to generate date select options in PHP forms?

When generating date select options in PHP forms using for loops, it is recommended to use the DateTime class to handle date calculations and formatting. This ensures that the dates are accurate and consistent. Additionally, using a for loop to iterate through a range of dates allows for dynamic generation of select options based on the desired date range.

<select name="date">
<?php
$currentYear = date('Y');
$startDate = new DateTime('today');
$endDate = new DateTime('+1 year');

for ($date = $startDate; $date <= $endDate; $date->modify('+1 day')) {
    echo '<option value="' . $date->format('Y-m-d') . '">' . $date->format('Y-m-d') . '</option>';
}
?>
</select>