How can PHP be used to create a dynamic calendar with clickable dates?

To create a dynamic calendar with clickable dates using PHP, you can use a combination of PHP and HTML to generate the calendar grid and make the dates clickable by passing the selected date as a parameter in the URL.

<?php
// Get the current year and month
$year = date("Y");
$month = date("n");

// Create a table for the calendar
echo "<table>";
echo "<tr><th colspan='7'>".date("F Y")."</th></tr>";
echo "<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";

// Get the first day of the month
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$dayOfWeek = date("w", $firstDay);

// Get the total number of days in the month
$totalDays = date("t", $firstDay);

// Create the calendar grid
echo "<tr>";
for ($i = 0; $i < $dayOfWeek; $i++) {
    echo "<td></td>";
}

for ($day = 1; $day <= $totalDays; $day++) {
    echo "<td><a href='calendar.php?date=$year-$month-$day'>$day</a></td>";
    $dayOfWeek++;
    if ($dayOfWeek % 7 == 0) {
        echo "</tr><tr>";
    }
}

echo "</tr></table>";
?>