How can DateTime be effectively used to handle date calculations for recurring events in PHP?

When handling date calculations for recurring events in PHP, the DateTime class can be effectively used to manage dates and calculate future occurrences of events. By setting the initial date and interval for the recurring event, you can easily calculate future dates based on the specified interval.

// Set initial date for the recurring event
$startDate = new DateTime('2022-01-01');

// Set interval for the recurring event (e.g., every month)
$interval = new DateInterval('P1M');

// Calculate future dates for the recurring event
for ($i = 0; $i < 5; $i++) {
    $nextDate = $startDate->add($interval);
    echo $nextDate->format('Y-m-d') . PHP_EOL;
}