PHP实现万年历

话不多说,直接看代码吧。

简单写的一个Demo,没有前后端分离,有时间你们可以实现一下,加个样式。

<?php

class TestController extends CController
{
    public function actionTest(){

        $year = $_GET['y']?$_GET['y']:date('Y');    //获取当前年
        $month = $_GET['m']?$_GET['m']:date('m');   //获取当前月
        $days = date('t',strtotime("$year-$month-1"));  //获取当前月天数
        $week = date('w',strtotime("$year-$month-1"));  //获取当前月一号是周几

        echo "<center>";
        echo "<h2>".$year."年".$month."月</h2>";
        echo "<table width='700px' border='1'>";
        //写个表头
        echo "<tr>
                    <th>日</th>
                    <th>一</th>
                    <th>二</th>
                    <th>三</th>
                    <th>四</th>
                    <th>五</th>
                    <th>六</th>
                  </tr>";
        for ($i = 1-$week;$i<=$days;){  //循环当前月的天数  1-$week是计算当前月的一号是在星期几的位置
            echo "<tr>";
            for ($j=0;$j<7;$j++){   //一周七天,内循环七次
                if ($i>$days || $i<1){  //如果循环完当前月的天数或者i为负数时,显示空白
                    echo "<td></td>";
                }else{
                    echo "<td>".$i."</td>";
                }
                $i++;
            }
            echo "</tr>";
        }
        echo "</table>";

        //实现上一年和上一月
        if ($month==1){
            $prevYear = $year-1;
            $prevMonth = 12;
        }else{
            $prevYear = $year;
            $prevMonth=$month-1;
        }

        //实现下一年和下一月
        if ($month==12){
            $nextYear = $year+1;
            $nextMonth = 1;
        }else{
            $nextYear = $year;
            $nextMonth = $month+1;
        }

        echo '<a href="/index.php?r=teacher/Test/test&y='.$prevYear.'&m='.$prevMonth.'">上一月</a>|<a href="/index.php?r=teacher/Test/test&y='.$nextYear.'&m='.$nextMonth.'">下一月</a>';

        echo "</center>";

    }
}
原文地址:https://www.cnblogs.com/zdigd/p/8884328.html