How can the issue of subtracting months using the date() function in PHP be resolved effectively?

Issue: The date() function in PHP does not provide a direct way to subtract months from a given date. To resolve this issue effectively, we can use the DateTime class along with DateInterval to subtract months from a date.

// Create a new DateTime object with the initial date
$date = new DateTime('2022-01-15');

// Subtract 3 months from the date
$date->sub(new DateInterval('P3M'));

// Format the date in the desired format
echo $date->format('Y-m-d');