What are the best practices for generating a monthly calendar in PHP?

Generating a monthly calendar in PHP involves determining the number of days in the month, the day of the week the month starts on, and then displaying each day in a grid format. One approach is to use the `cal_days_in_month()` function to get the number of days in the month and the `date()` function to determine the day of the week the month starts on.

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

// Get the number of days in the month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);

// Get the day of the week the month starts on
$first_day_of_month = date('N', strtotime("$year-$month-01"));

// Display the calendar
echo "<table>";
echo "<tr><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th><th>Sun</th></tr>";
echo "<tr>";
for ($i = 1; $i < $first_day_of_month; $i++) {
    echo "<td></td>";
}
for ($day = 1; $day <= $days_in_month; $day++) {
    echo "<td>$day</td>";
    if (($day + $first_day_of_month - 1) % 7 == 0) {
        echo "</tr><tr>";
    }
}
echo "</tr>";
echo "</table>";
?>