当前位置:首页 > PHP

php实现一个日历表

2026-02-28 05:13:59PHP

实现PHP日历表的方法

使用PHP内置函数生成日历

PHP内置的cal_days_in_monthdate函数可以方便地生成日历。以下代码创建一个基本的月历视图:

php实现一个日历表

function generateCalendar($year, $month) {
    $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $firstDay = date('N', strtotime("$year-$month-01"));

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

    $dayCount = 1;
    echo "<tr>";
    for ($i = 1; $i < $firstDay; $i++) {
        echo "<td></td>";
    }

    for ($i = $firstDay; $i <= 7; $i++) {
        echo "<td>$dayCount</td>";
        $dayCount++;
    }
    echo "</tr>";

    while ($dayCount <= $daysInMonth) {
        echo "<tr>";
        for ($i = 1; $i <= 7 && $dayCount <= $daysInMonth; $i++) {
            echo "<td>$dayCount</td>";
            $dayCount++;
        }
        echo "</tr>";
    }
    echo "</table>";
}

generateCalendar(date('Y'), date('m'));

添加导航功能

扩展日历功能,添加月份和年份导航:

php实现一个日历表

function calendarWithNavigation() {
    $year = isset($_GET['year']) ? $_GET['year'] : date('Y');
    $month = isset($_GET['month']) ? $_GET['month'] : date('m');

    $prevMonth = $month - 1;
    $prevYear = $year;
    if ($prevMonth < 1) {
        $prevMonth = 12;
        $prevYear--;
    }

    $nextMonth = $month + 1;
    $nextYear = $year;
    if ($nextMonth > 12) {
        $nextMonth = 1;
        $nextYear++;
    }

    echo "<div style='text-align:center;'>";
    echo "<a href='?year=$prevYear&month=$prevMonth'>Previous</a> ";
    echo date('F Y', strtotime("$year-$month-01"));
    echo " <a href='?year=$nextYear&month=$nextMonth'>Next</a>";
    echo "</div>";

    generateCalendar($year, $month);
}

calendarWithNavigation();

高亮当前日期

修改日历函数,突出显示当前日期:

function generateCalendarWithHighlight($year, $month) {
    $currentDay = date('j');
    $currentMonth = date('n');
    $currentYear = date('Y');

    $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $firstDay = date('N', strtotime("$year-$month-01"));

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

    $dayCount = 1;
    echo "<tr>";
    for ($i = 1; $i < $firstDay; $i++) {
        echo "<td></td>";
    }

    for ($i = $firstDay; $i <= 7; $i++) {
        $highlight = ($dayCount == $currentDay && $month == $currentMonth && $year == $currentYear) ? "style='background-color:yellow;'" : "";
        echo "<td $highlight>$dayCount</td>";
        $dayCount++;
    }
    echo "</tr>";

    while ($dayCount <= $daysInMonth) {
        echo "<tr>";
        for ($i = 1; $i <= 7 && $dayCount <= $daysInMonth; $i++) {
            $highlight = ($dayCount == $currentDay && $month == $currentMonth && $year == $currentYear) ? "style='background-color:yellow;'" : "";
            echo "<td $highlight>$dayCount</td>";
            $dayCount++;
        }
        echo "</tr>";
    }
    echo "</table>";
}

使用DateTime类实现

更现代的PHP实现方式,使用DateTime类:

function calendarUsingDateTime($year, $month) {
    $date = new DateTime("$year-$month-01");
    $daysInMonth = $date->format('t');
    $firstDay = $date->format('N');

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

    $dayCount = 1;
    echo "<tr>";
    for ($i = 1; $i < $firstDay; $i++) {
        echo "<td></td>";
    }

    for ($i = $firstDay; $i <= 7; $i++) {
        echo "<td>$dayCount</td>";
        $dayCount++;
    }
    echo "</tr>";

    while ($dayCount <= $daysInMonth) {
        echo "<tr>";
        for ($i = 1; $i <= 7 && $dayCount <= $daysInMonth; $i++) {
            echo "<td>$dayCount</td>";
            $dayCount++;
        }
        echo "</tr>";
    }
    echo "</table>";
}

这些方法提供了不同复杂度的PHP日历实现,从基本功能到包含导航和高亮功能的完整日历系统。可以根据项目需求选择适合的实现方式或进行组合使用。

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

相关文章

php实现排序

php实现排序

PHP 数组排序方法 PHP 提供了多种内置函数用于数组排序,适用于不同的排序需求。以下是常见的排序方法: 按值升序排序 sort() 函数对数组按值升序排序,重置键名(索引数组适用): $num…

php 实现跳转

php 实现跳转

PHP 实现页面跳转的方法 在PHP中,实现页面跳转可以通过多种方式完成,以下是几种常用的方法: 使用header函数 header函数是PHP中最常用的跳转方法,通过发送HTTP头部信息实现跳转。…

php mvc 实现

php mvc 实现

PHP MVC 实现方法 MVC(Model-View-Controller)是一种设计模式,用于分离应用程序的逻辑、数据和界面。以下是实现PHP MVC框架的关键步骤。 目录结构 创建以下目录结构…

php 链表实现

php 链表实现

在 PHP 中实现链表可以通过类来模拟链表节点(Node)和链表(LinkedList)结构。以下是链表的完整实现示例,包含基础操作如插入、删除、遍历等。 链表节点类(Node) 链表的基本单元是节…

php 实现锁

php 实现锁

PHP 实现锁的常见方法 文件锁(flock) 利用 flock 函数对文件进行独占锁定,适用于单机环境。 $fp = fopen("lockfile.lock", "w+"); if (f…

php redis 队列实现

php redis 队列实现

PHP Redis 队列实现 Redis 是一个高性能的键值存储系统,常被用作消息队列。以下是使用 PHP 和 Redis 实现队列的几种方法。 安装依赖 确保已安装 PHP Redis 扩展。可以…