How can the getdate() function in PHP be used to retrieve past or future dates for a calendar program?

To retrieve past or future dates for a calendar program using the getdate() function in PHP, you can manipulate the timestamp obtained from the function by adding or subtracting the desired number of days. This can be achieved by using the mktime() function to create a new timestamp based on the current date and then converting it back to a date format using date().

// Get the current date
$currentDate = getdate();

// Calculate past date (e.g. 7 days ago)
$pastDate = date("Y-m-d", mktime(0, 0, 0, $currentDate['mon'], $currentDate['mday'] - 7, $currentDate['year']));

// Calculate future date (e.g. 7 days from now)
$futureDate = date("Y-m-d", mktime(0, 0, 0, $currentDate['mon'], $currentDate['mday'] + 7, $currentDate['year']));

echo "Past Date: " . $pastDate . "<br>";
echo "Future Date: " . $futureDate;