How can PHP be used to efficiently highlight weekends, holidays, and events in a calendar?
To efficiently highlight weekends, holidays, and events in a calendar using PHP, you can create arrays containing the dates for weekends, holidays, and events. Then, when generating the calendar, you can check if the current date is in any of these arrays and apply different styles or colors to highlight them accordingly.
$weekend_dates = array('2022-01-01', '2022-01-02', '2022-01-08', '2022-01-09', '2022-01-15', '2022-01-16');
$holiday_dates = array('2022-01-01', '2022-01-15');
$event_dates = array('2022-01-10', '2022-01-20');
$current_date = date('Y-m-d');
if (in_array($current_date, $weekend_dates)) {
echo '<td style="background-color: yellow;">' . $current_date . '</td>';
} elseif (in_array($current_date, $holiday_dates)) {
echo '<td style="background-color: red;">' . $current_date . '</td>';
} elseif (in_array($current_date, $event_dates)) {
echo '<td style="background-color: blue;">' . $current_date . '</td>';
} else {
echo '<td>' . $current_date . '</td>';
}