Home > php教程 > php手册 > PHP练手:日历(代码简单,扩展容易)

PHP练手:日历(代码简单,扩展容易)

WBOY
Release: 2016-06-21 08:47:49
Original
1641 people have browsed it

抽空写了个日历程序,只注重功能和实现的思路,所以代码和功能都比较简单,但是理解和扩展也比较容易。

show()函数用来显示日历,你可以修改show()函数,通过传值的方式来实现显示不同的年月。

<?php
    class Calendar{
        public $weekarray = array(&#39;星期日&#39;,&#39;星期一&#39;,&#39;星期二&#39;,&#39;星期三&#39;,&#39;星期四&#39;,&#39;星期五&#39;,&#39;星期六&#39;);
        public $firstDay = &#39;&#39;;//当月第一天
        public $firstNum = &#39;&#39;;//返回当月第一天对应的星期数字
        public $firstDayNum = &#39;&#39;;//当月第一天对应的中文星期
        
        /**
        *   指定日是星期几
        *   eg:$date=&#39;2014-07-21&#39;
        */
        public function getWeek($date){
            $date = strtotime($date);//strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳
            $num = date(&#39;w&#39;,$date);//数字型的星期几,如: "0" (星期日) 至 "6" (星期六)
            return $num;
        }

        /**
        *   取得指定月有多少天
        *   eg:$month = &#39;2014-07&#39;
        */
        public function getMonthNum($month){
            $month = strtotime($month);
            return date(&#39;t&#39;,$month);//指定月份的天数
        }
        
        /**
        *   显示日历
        */
        public function show(){
            //取得当前日期
            $year = date(&#39;Y&#39;);
            $month = date(&#39;m&#39;);
            $day = date(&#39;d&#39;);
            echo &#39;<table border="1" style="text-align:center;">&#39;;
            echo &#39;<tr><td colspan="7">&#39;.$year.&#39;-&#39;.$month.&#39;</td></tr>&#39;;
            echo &#39;<tr><th>星期日</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th></tr>&#39;;
            //取得当前月有多少天
            $yearMonth = &#39;$year."-".$month&#39;;
            $monthNum = $this->getMonthNum($yearMonth);
            //取得当前月第一天是星期几
            $this->firstDay = $year."-".$month."-01";
            $this->firstNum = $this->getWeek($this->firstDay);
            $this->firstDayNum = $this->weekarray[$this->firstNum];
            
            echo &#39;<tr>&#39;;
                for($i=1;$i<=$monthNum+($this->firstNum);$i++){
                    echo &#39;<td>&#39;;
                    if($i >= $this->firstNum+1){
                        $a = $i-$this->firstNum;
                        if($a == $day){
                            echo &#39;<p style="background-color:blue;color:#fff;">&#39;.$a.&#39;</p>&#39;;
                        }else{
                            echo &#39;<p>&#39;.$a.&#39;</p>&#39;;
                        }
                    }
                    echo &#39;</td>&#39;;
                    if($i%7 == 0){//每输出7列就换一行
                        echo &#39;</tr>&#39;;
                    }
                }
            echo &#39;</table>&#39;;
        }

    }
    
    $calendar = new Calendar();
    $calendar->show();
Copy after login

效果图:

PHP date()函数的使用

这里有篇date()函数的详解:http://wenku.baidu.com/link?url=OPDGzaCMWgjDE0ya8QlDbLIXX0c11ohUjsoLyRm-cYp7lz-O_7H4XBILv8JfomhbnSxXSW0FhqxYBK0_gn8Nr77XMWp-_st7v8AYecbNZjG



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template