使用PHP写一段日历的代码
时间: 2023-03-30 10:03:25 浏览: 81
以下是使用 PHP 编写的简单日历代码:
```php
<?php
// 获取当前年份和月份
$year = date('Y');
$month = date('m');
// 获取本月天数和第一天是星期几
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$first_day = date('w', strtotime("$year-$month-01"));
// 输出日历表格
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>";
// 输出日期
$day_count = 1;
echo "<tr>";
for ($i = ; $i < 7; $i++) {
if ($i < $first_day) {
echo "<td></td>";
} else {
echo "<td>$day_count</td>";
$day_count++;
}
}
echo "</tr>";
while ($day_count <= $days_in_month) {
echo "<tr>";
for ($i = ; $i < 7; $i++) {
if ($day_count > $days_in_month) {
echo "<td></td>";
} else {
echo "<td>$day_count</td>";
$day_count++;
}
}
echo "</tr>";
}
echo "</table>";
?>
```
这段代码会输出当前月份的日历表格,包括星期几和日期。你可以根据需要进行修改和美化。