当前位置:首页 > PHP

php日历实现

2026-02-28 10:44:49PHP

实现 PHP 日历的基本方法

使用 PHP 创建一个简单的日历功能,可以通过以下步骤完成。日历可以显示当前月份的日期,并支持前后月份导航。

获取当前月份和年份

$month = date('m');
$year = date('Y');

计算月份的天数和第一天是星期几

php日历实现

$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$first_day_of_month = 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>';

$day_count = 1;
echo '<tr>';
for ($i = 1; $i < $first_day_of_month; $i++) {
    echo '<td></td>';
}

for ($i = $first_day_of_month; $i <= 7; $i++) {
    echo '<td>' . $day_count . '</td>';
    $day_count++;
}
echo '</tr>';

while ($day_count <= $days_in_month) {
    echo '<tr>';
    for ($i = 1; $i <= 7 && $day_count <= $days_in_month; $i++) {
        echo '<td>' . $day_count . '</td>';
        $day_count++;
    }
    echo '</tr>';
}
echo '</table>';

添加月份导航功能

处理导航参数

php日历实现

if (isset($_GET['month']) && isset($_GET['year'])) {
    $month = $_GET['month'];
    $year = $_GET['year'];
} else {
    $month = date('m');
    $year = date('Y');
}

生成导航链接

$prev_month = $month - 1;
$prev_year = $year;
if ($prev_month < 1) {
    $prev_month = 12;
    $prev_year--;
}

$next_month = $month + 1;
$next_year = $year;
if ($next_month > 12) {
    $next_month = 1;
    $next_year++;
}

echo '<a href="?month=' . $prev_month . '&year=' . $prev_year . '">Previous</a>';
echo ' ';
echo '<a href="?month=' . $next_month . '&year=' . $next_year . '">Next</a>';

高亮当前日期

标记当前日期

$current_day = date('j');
if ($day_count == $current_day && $month == date('m') && $year == date('Y')) {
    echo '<td style="background-color: yellow;">' . $day_count . '</td>';
} else {
    echo '<td>' . $day_count . '</td>';
}

完整代码示例

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

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

$prev_month = $month - 1;
$prev_year = $year;
if ($prev_month < 1) {
    $prev_month = 12;
    $prev_year--;
}

$next_month = $month + 1;
$next_year = $year;
if ($next_month > 12) {
    $next_month = 1;
    $next_year++;
}

echo '<a href="?month=' . $prev_month . '&year=' . $prev_year . '">Previous</a>';
echo ' ';
echo '<a href="?month=' . $next_month . '&year=' . $next_year . '">Next</a>';

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>';

$day_count = 1;
echo '<tr>';
for ($i = 1; $i < $first_day_of_month; $i++) {
    echo '<td></td>';
}

for ($i = $first_day_of_month; $i <= 7 && $day_count <= $days_in_month; $i++) {
    $current_date = ($day_count == date('j') && $month == date('m') && $year == date('Y')) ? 
        '<td style="background-color: yellow;">' . $day_count . '</td>' : 
        '<td>' . $day_count . '</td>';
    echo $current_date;
    $day_count++;
}
echo '</tr>';

while ($day_count <= $days_in_month) {
    echo '<tr>';
    for ($i = 1; $i <= 7 && $day_count <= $days_in_month; $i++) {
        $current_date = ($day_count == date('j') && $month == date('m') && $year == date('Y')) ? 
            '<td style="background-color: yellow;">' . $day_count . '</td>' : 
            '<td>' . $day_count . '</td>';
        echo $current_date;
        $day_count++;
    }
    echo '</tr>';
}
echo '</table>';
?>

这段代码实现了一个基本的 PHP 日历功能,包括月份导航和当前日期高亮显示。可以根据需要进一步扩展,例如添加事件管理或更复杂的样式。

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

相关文章

vue 实现日历

vue 实现日历

实现基础日历结构 使用 Vue 的模板语法构建日历的网格布局,通常以 7 列(一周)和 5-6 行(月份天数)的表格形式展示。通过 v-for 循环渲染日期单元格,动态绑定样式和事件。…

基于php的设计与实现

基于php的设计与实现

PHP 设计与实现的关键要素 环境搭建与框架选择 PHP开发需配置服务器环境(如Apache/Nginx)、PHP版本(建议7.4+)及数据库(MySQL/MariaDB)。主流框架包括Laravel…

php 实现秒杀

php 实现秒杀

PHP 实现秒杀的关键技术 秒杀系统需要解决高并发、超卖、性能瓶颈等问题。以下是实现秒杀的核心方案: 使用 Redis 预减库存 将商品库存提前加载到 Redis 中,通过原子操作保证库存准确性:…

php搜索功能实现

php搜索功能实现

实现基本的PHP搜索功能 使用MySQL数据库和PHP实现简单的关键词搜索功能。需要确保数据库中有可供搜索的表和数据。 // 连接数据库 $conn = new mysqli('localhost'…

php实现下载

php实现下载

PHP实现文件下载 使用PHP实现文件下载可以通过设置HTTP头部信息强制浏览器下载文件而非直接打开。以下是几种常见的方法: 方法一:直接输出文件内容 $file_path = 'path/to/…

php 实现评论

php 实现评论

实现 PHP 评论功能 数据库设计 创建数据库表存储评论数据,通常包含以下字段: id:评论唯一标识 user_id:用户ID content:评论内容 created_at:评论时间 parent…