How can PHP be used to dynamically generate date ranges for weekly calendar views based on user interactions with Next and Previous buttons?
To dynamically generate date ranges for weekly calendar views based on user interactions with Next and Previous buttons, you can store the current date range in a session variable and update it accordingly when the user clicks on the Next or Previous buttons. You can then use PHP date functions to calculate the start and end dates of the week based on the current date range.
<?php
session_start();
// Initialize start and end dates for the current week
if (!isset($_SESSION['start_date'])) {
$_SESSION['start_date'] = date('Y-m-d', strtotime('monday this week'));
}
if (!isset($_SESSION['end_date'])) {
$_SESSION['end_date'] = date('Y-m-d', strtotime('sunday this week'));
}
// Update date range based on user interactions
if (isset($_POST['next'])) {
$_SESSION['start_date'] = date('Y-m-d', strtotime($_SESSION['start_date'] . ' +1 week'));
$_SESSION['end_date'] = date('Y-m-d', strtotime($_SESSION['end_date'] . ' +1 week'));
}
if (isset($_POST['previous'])) {
$_SESSION['start_date'] = date('Y-m-d', strtotime($_SESSION['start_date'] . ' -1 week'));
$_SESSION['end_date'] = date('Y-m-d', strtotime($_SESSION['end_date'] . ' -1 week'));
}
// Display the current week's date range
echo "Week of " . date('F j, Y', strtotime($_SESSION['start_date'])) . " - " . date('F j, Y', strtotime($_SESSION['end_date']));
// Additional code to display the weekly calendar using the date range
?>