Home >Backend Development >PHP Tutorial >PHP Practice: Calendar (Simple Code, Easy to Expand)_PHP Tutorial

PHP Practice: Calendar (Simple Code, Easy to Expand)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:28917browse

PHP practice: Calendar (simple code, easy to expand)

I took the time to write a calendar program, focusing only on functions and implementation ideas, so the code and functions are relatively simple, but it is also easier to understand and expand.

The show() function is used to display the calendar. You can modify the show() function to display different years and months by passing values.

<?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;<div style="background-color:blue;color:#fff;">&#39;.$a.&#39;</div>&#39;;
                        }else{
                            echo &#39;<div>&#39;.$a.&#39;</div>&#39;;
                        }
                    }
                    echo &#39;</td>&#39;;
                    if($i%7 == 0){//每输出7列就换一行
                        echo &#39;</tr>&#39;;
                    }
                }
            echo &#39;</table>&#39;;
        }

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

Rendering:

PHP Practice: Calendar (Simple Code, Easy to Expand)_PHP TutorialUsage of PHP date() function

Here is a detailed explanation of the date() function: http://wenku.baidu.com/link?url=OPDGzaCMWgjDE0ya8QlDbLIXX0c11ohUjsoLyRm-cYp7lz-O_7H4XBILv8JfomhbnSxXSW0FhqxYBK0_gn8Nr77XMWp-_st7v8AYecbNZjG

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/847853.htmlTechArticlePHP practice: Calendar (simple code, easy to expand) I took the time to write a calendar program, focusing only on functions and implementation. ideas, so the code and functions are relatively simple, but understanding and expansion are also easier than...
Statement:
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