当前位置:首页 > PHP

php实现日历周末变红

2026-01-29 05:22:46PHP

实现日历周末变红的方法

使用PHP实现日历中周末日期显示为红色,可以通过判断日期是否为周六或周日,然后应用CSS样式来实现。以下是具体步骤:

获取当前月份的天数和第一天是星期几

$year = date('Y');
$month = date('m');
$days_in_month = date('t', mktime(0, 0, 0, $month, 1, $year));
$first_day = date('w', mktime(0, 0, 0, $month, 1, $year));

生成日历表格

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>';
$day_count = 1;
echo '<tr>';
for ($i = 0; $i < 7; $i++) {
    if ($i < $first_day) {
        echo '<td></td>';
    } else {
        $current_date = "$year-$month-$day_count";
        $day_of_week = date('w', strtotime($current_date));
        $style = ($day_of_week == 0 || $day_of_week == 6) ? 'style="color:red;"' : '';
        echo "<td $style>$day_count</td>";
        $day_count++;
    }
}
echo '</tr>';

填充剩余日期

while ($day_count <= $days_in_month) {
    echo '<tr>';
    for ($i = 0; $i < 7; $i++) {
        if ($day_count > $days_in_month) {
            echo '<td></td>';
        } else {
            $current_date = "$year-$month-$day_count";
            $day_of_week = date('w', strtotime($current_date));
            $style = ($day_of_week == 0 || $day_of_week == 6) ? 'style="color:red;"' : '';
            echo "<td $style>$day_count</td>";
            $day_count++;
        }
    }
    echo '</tr>';
}
echo '</table>';

使用CSS样式优化

可以在HTML头部定义CSS样式,使代码更清晰:

<style>
    .weekend {
        color: red;
    }
</style>

然后在PHP代码中动态添加类:

$day_of_week = date('w', strtotime($current_date));
$class = ($day_of_week == 0 || $day_of_week == 6) ? 'weekend' : '';
echo "<td class='$class'>$day_count</td>";

完整示例代码

<?php
$year = date('Y');
$month = date('m');
$days_in_month = date('t', mktime(0, 0, 0, $month, 1, $year));
$first_day = date('w', mktime(0, 0, 0, $month, 1, $year));
?>
<style>
    .weekend {
        color: red;
    }
    table {
        border-collapse: collapse;
    }
    td, th {
        padding: 8px;
        text-align: center;
    }
</style>
<table border="1">
    <tr>
        <th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th>
    </tr>
    <tr>
        <?php
        $day_count = 1;
        for ($i = 0; $i < 7; $i++) {
            if ($i < $first_day) {
                echo '<td></td>';
            } else {
                $current_date = "$year-$month-$day_count";
                $day_of_week = date('w', strtotime($current_date));
                $class = ($day_of_week == 0 || $day_of_week == 6) ? 'weekend' : '';
                echo "<td class='$class'>$day_count</td>";
                $day_count++;
            }
        }
        ?>
    </tr>
    <?php
    while ($day_count <= $days_in_month) {
        echo '<tr>';
        for ($i = 0; $i < 7; $i++) {
            if ($day_count > $days_in_month) {
                echo '<td></td>';
            } else {
                $current_date = "$year-$month-$day_count";
                $day_of_week = date('w', strtotime($current_date));
                $class = ($day_of_week == 0 || $day_of_week == 6) ? 'weekend' : '';
                echo "<td class='$class'>$day_count</td>";
                $day_count++;
            }
        }
        echo '</tr>';
    }
    ?>
</table>

注意事项

  • 代码中的date('w')返回星期几的数字,0表示周日,6表示周六。
  • 可以根据需要调整CSS样式,比如改变字体大小、背景色等。
  • 如果需要显示其他月份的日历,可以修改$year$month变量的值。

php实现日历周末变红

标签: 变红日历
分享给朋友:

相关文章

vue实现日历效果

vue实现日历效果

Vue实现日历效果的方法 使用第三方库 Vue生态中有多个成熟的日历组件库,如v-calendar或vue2-calendar,可以快速实现日历功能。安装后直接引入组件即可使用。 npm inst…

vue表格实现日历

vue表格实现日历

Vue 表格实现日历的方法 基础表格结构 使用 el-table 或原生 HTML 表格,通过 v-for 循环生成日历格子。月份天数通过 new Date() 计算,动态渲染表格内容。 &…

vue日历拖拽实现

vue日历拖拽实现

Vue 日历拖拽实现方案 使用 vue-draggable 库 安装依赖库: npm install vuedraggable 基础拖拽日历实现代码: <template> &…

基于vue实现日历

基于vue实现日历

实现基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含星期标题和日期格子。月份切换按钮通过v-on绑定事件。 <template> <div class="ca…

vue如何实现日历

vue如何实现日历

使用第三方库(如 FullCalendar) FullCalendar 是一个功能强大的日历库,支持 Vue 集成。安装依赖后,通过组件的方式引入日历功能。配置事件、日期范围和交互逻辑可以通过 pro…

vue实现搜索框变红

vue实现搜索框变红

实现搜索框变红的方法 在Vue中实现搜索框变红可以通过动态绑定样式或类名来实现。以下是几种常见的方法: 使用动态类名绑定 通过v-bind:class绑定一个类名,当满足条件时添加红色边框样式。…