当前位置:首页 > PHP

php 实现日历

2026-01-29 18:51:34PHP

使用 PHP 实现日历

基础日历实现

PHP 可以通过日期函数和循环结构实现一个简单的日历。以下是一个基础示例:

<?php
function generateCalendar($year, $month) {
    $firstDay = mktime(0, 0, 0, $month, 1, $year);
    $daysInMonth = date("t", $firstDay);
    $dayOfWeek = date("w", $firstDay);

    echo "<table border='1'>";
    echo "<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";

    echo "<tr>";
    for ($i = 0; $i < $dayOfWeek; $i++) {
        echo "<td></td>";
    }

    for ($day = 1; $day <= $daysInMonth; $day++) {
        if (($day + $dayOfWeek - 1) % 7 == 0 && $day != 1) {
            echo "</tr><tr>";
        }
        echo "<td>$day</td>";
    }

    echo "</tr></table>";
}

generateCalendar(2023, 11);
?>

带样式的日历

可以添加 CSS 样式使日历更美观:

php 实现日历

<style>
.calendar {
    font-family: Arial, sans-serif;
    border-collapse: collapse;
    width: 300px;
}
.calendar th {
    background-color: #f2f2f2;
    text-align: center;
    padding: 8px;
}
.calendar td {
    border: 1px solid #ddd;
    text-align: center;
    padding: 8px;
}
.calendar td:hover {
    background-color: #f5f5f5;
}
</style>

可导航的日历

添加月份导航功能:

php 实现日历

<?php
$currentMonth = isset($_GET['month']) ? $_GET['month'] : date('n');
$currentYear = isset($_GET['year']) ? $_GET['year'] : date('Y');

function getNextMonth($month, $year) {
    if ($month == 12) {
        return [1, $year + 1];
    }
    return [$month + 1, $year];
}

function getPrevMonth($month, $year) {
    if ($month == 1) {
        return [12, $year - 1];
    }
    return [$month - 1, $year];
}

list($prevMonth, $prevYear) = getPrevMonth($currentMonth, $currentYear);
list($nextMonth, $nextYear) = getNextMonth($currentMonth, $currentYear);
?>

<a href="?month=<?= $prevMonth ?>&year=<?= $prevYear ?>">Previous</a>
<span><?= date('F Y', mktime(0, 0, 0, $currentMonth, 1, $currentYear)) ?></span>
<a href="?month=<?= $nextMonth ?>&year=<?= $nextYear ?>">Next</a>

<?php generateCalendar($currentYear, $currentMonth); ?>

带事件的日历

可以扩展日历以显示事件:

$events = [
    '2023-11-15' => 'Team Meeting',
    '2023-11-20' => 'Project Deadline'
];

function generateCalendarWithEvents($year, $month, $events) {
    // 前面的日历生成代码...

    for ($day = 1; $day <= $daysInMonth; $day++) {
        $date = sprintf("%04d-%02d-%02d", $year, $month, $day);
        $event = isset($events[$date]) ? $events[$date] : '';

        if (($day + $dayOfWeek - 1) % 7 == 0 && $day != 1) {
            echo "</tr><tr>";
        }
        echo "<td>$day<br><small>$event</small></td>";
    }

    // 后面的日历生成代码...
}

使用 DateTime 类

现代 PHP 推荐使用 DateTime 类:

function generateCalendarWithDateTime($year, $month) {
    $date = new DateTime("$year-$month-01");
    $daysInMonth = $date->format('t');
    $firstDayOfWeek = $date->format('w');

    // 其余日历生成逻辑类似
}

这些方法展示了如何使用 PHP 创建各种功能的日历,从基本显示到添加导航和事件功能。

标签: 日历php
分享给朋友:

相关文章

php实现分页

php实现分页

PHP实现分页的基本方法 使用原生PHP实现分页功能需要结合数据库查询和HTML展示。以下是核心实现步骤: 数据库查询与分页逻辑 // 连接数据库 $conn = new mysqli('loca…

php实现定时任务

php实现定时任务

PHP 实现定时任务的几种方法 在 PHP 中实现定时任务可以通过多种方式完成,具体选择取决于项目需求和服务器环境。以下是常见的实现方法: 使用 Cron 任务 Cron 是 Linux 系统中常…

php验证码实现

php验证码实现

验证码实现方法 在PHP中实现验证码功能通常涉及生成随机字符串或数字,将其转换为图像,并通过会话(Session)进行验证。以下是几种常见的实现方式: 使用GD库生成图像验证码 GD库是PHP内置的…

php 路由实现

php 路由实现

PHP 路由实现方法 在 PHP 中实现路由功能有多种方式,以下是几种常见的实现方法: 使用原生 PHP 实现 通过解析 URL 并匹配对应的控制器和动作: $request = $_SERV…

php实现注册

php实现注册

注册功能实现步骤 数据库准备 创建用户表,包含字段如id、username、password、email等。示例SQL: CREATE TABLE users ( id INT AUTO_…

php实现mysql

php实现mysql

PHP 连接 MySQL 的基本方法 PHP 提供了多种方式与 MySQL 数据库进行交互,以下是最常用的几种方法。 使用 MySQLi 扩展 MySQLi 是 MySQL 的改进扩展,支持面…