<?php
header('Content-Type: text/html; charset=utf-8');
// 设置时区
date_default_timezone_set('Asia/Shanghai');
// 设置年份
$year = 2023;
// 设置月份及每个月的天数
$months = array(
'01' => 31,
'02' => 28,
'03' => 31,
'04' => 30,
'05' => 31,
'06' => 30,
'07' => 31,
'08' => 31,
'09' => 30,
'10' => 31,
'11' => 30,
'12' => 31
);
// 判断是否是闰年
if (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) {
$months['02'] = 29;
}
// 循环生成每个月的日历
foreach ($months as $month => $days) {
// 获取本月第一天是星期几
$first_day_of_month = date('w', strtotime("$year-$month-01"));
// 输出月份和标题
echo "<table style='border:1px solid #ccc;margin-bottom:30px'>";
echo "<tr><th colspan='7'>$year 年 $month 月</th></tr>";
// 输出星期几的标题
echo "<tr>";
echo "<th style='background-color:#FFD700'>日</th>";
echo "<th>一</th>";
echo "<th>二</th>";
echo "<th>三</th>";
echo "<th>四</th>";
echo "<th>五</th>";
echo "<th style='background-color:#FFD700'>六</th>";
echo "</tr>";
// 输出日期
$current_day = 1;
$row = 0;
while ($current_day <= $days) {
echo "<tr>";
for ($col = 0; $col < 7; $col++) {
if ($col == 0 || $col == 6) {
echo "<td style='background-color:#FFD700'>";
} else {
echo "<td>";
}
if (($row == 0 && $col < $first_day_of_month) || $current_day > $days) {
echo " ";
} else {
echo "$current_day";
$current_day++;
}
echo "</td>";
}
echo "</tr>";
$row++;
}
echo "</table>";
}
?>