How can PHP be used to dynamically highlight dates on a calendar based on the number of reservations in a database?

To dynamically highlight dates on a calendar based on the number of reservations in a database, we can query the database for the number of reservations for each date and then use PHP to generate the calendar HTML with the appropriate CSS classes to highlight the dates with reservations.

<?php
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Query the database to get the number of reservations for each date
$stmt = $pdo->prepare("SELECT date, COUNT(*) as num_reservations FROM reservations GROUP BY date");
$stmt->execute();
$reservations = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Generate the calendar HTML
echo '<table>';
for ($i = 1; $i <= 31; $i++) {
    $date = date('Y-m-d', strtotime("2023-01-$i"));
    $num_reservations = 0;
    foreach ($reservations as $reservation) {
        if ($reservation['date'] == $date) {
            $num_reservations = $reservation['num_reservations'];
            break;
        }
    }
    $highlight_class = $num_reservations > 0 ? 'highlight' : '';
    echo "<td class='$highlight_class'>$i</td>";
    if ($i % 7 == 0) {
        echo '</tr><tr>';
    }
}
echo '</table>';
?>