PHP練手:日曆(代碼簡單,擴充容易)

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   2014   

        抽空寫了個行事曆程式,只注重功能和實現的思路,所以代碼和功能都比較簡單,但是理解和擴充也比較容易。

        show()函數用來顯示日曆,你可以修改show()函數,通過傳值的方式來實現顯示不同的年月。

<?php    class Calendar{        public $weekarray = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');        public $firstDay = '';//當月第一天        public $firstNum = '';//返回當月第一天對應的星期數字        public $firstDayNum = '';//當月第一天對應的中文星期                /**        *   指定日是星期幾        *   eg:$date='2014-07-21'        */        public function getWeek($date){            $date = strtotime($date);//strtotime() 函數將任何英文文本的日期時間描述解析為 Unix 時間戳記            $num = date('w',$date);//數字型的星期幾,如: "0" (星期日) 至 "6" (星期六)            return $num;        }        /**        *   取得指定月有多少天        *   eg:$month = '2014-07'        */        public function getMonthNum($month){            $month = strtotime($month);            return date('t',$month);//指定月份的天數        }                /**        *   顯示日曆        */        public function show(){            //取得當前日期            $year = date('Y');            $month = date('m');            $day = date('d');            echo '<table border="1" style="text-align:center;">';            echo '<tr><td colspan="7">'.$year.'-'.$month.'</td></tr>';            echo '<tr><th>星期日</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th></tr>';            //取得當前月有多少天            $yearMonth = '$year."-".$month';            $monthNum = $this->getMonthNum($yearMonth);            //取得當前月第一天是星期幾            $this->firstDay = $year."-".$month."-01";            $this->firstNum = $this->getWeek($this->firstDay);            $this->firstDayNum = $this->weekarray[$this->firstNum];                        echo '<tr>';                for($i=1;$i<=$monthNum+($this->firstNum);$i++){                    echo '<td>';                    if($i >= $this->firstNum+1){                        $a = $i-$this->firstNum;                        if($a == $day){                            echo '<div style="background-color:blue;color:#fff;">'.$a.'</div>';                        }else{                            echo '<div>'.$a.'</div>';                        }                    }                    echo '</td>';                    if($i%7 == 0){//每輸出7列就換一行                        echo '</tr>';                    }                }            echo '</table>';        }    }        $calendar = new Calendar();    $calendar->show();

:


        show()函數用來顯示日曆,你可以修改show()函數,通過傳值的方式來實現顯示不同的年月。

擴充:

本文的主要知識點在於PHP date()函數的使用

這裡有篇date()函數的詳解:http://wenku.baidu.com/link?url=OPDGzaCMWgjDE0ya8QlDbLIXX0c11ohUjsoLyRm-cYp7lz-O_7H4XBILv8JfomhbnSxXSW0FhqxYBK0_gn8Nr77XMWp-_st7v8AYecbNZjG

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.