一個比較不錯的PHP日曆類分享_php執行個體

來源:互聯網
上載者:User

說到對時期和時間的處理,就一定要介紹一下行事曆程式的編寫。但一提起編寫日曆,大多數人都會認為日曆的作用只是為了在頁上顯示當前的日期,其實日曆在我們的開發中有更重要的作用。例如我們開發一個“記事本”就需要通過日曆設定日期,還有一些系統中需要按日期去排任務,也需要日曆,等等。本例涉及的日期和時間函數並不是很多,都是前面介紹的內容,主要是通過一個日曆類的編寫,鞏固一下前面介紹過的物件導向的文法知識,以及時間函數應用,最主要的是可以提升初學者的思維邏輯和程式設計能力。將日曆類Calendar聲明在檔案calendar.class.php中,代碼如下所示:

<?php  //     file:calendar.class.php   日曆類原檔案   error_reporting(0);     class Calendar{        private $year;        private $month;        private $start_weekday;            //當月的第一天對應的是周幾,作為當月開始遍曆日期的開始        private $days;                 //當前月總天數         //構造方法,用來初使化一些日期屬性        function __construct(){            //如果使用者沒有設定所份數,則使用當前系統時間的年份            $this->year = isset($_GET["year"]) ? $_GET["year"] : date("Y");            //如果使用者沒有設定月份數,則使用當前系統時間的月份            $this->month = isset($_GET["month"]) ? $_GET["month"] : date("m");            //通過具體的年份和月份,利用date()函數的w參數擷取當月第一天對應的是周幾            $this->start_weekday = date("w",mktime(0,0,0,$this->month,1,$this->year));            //通過具體的年份和月份,利用date()函數的t參數擷取當月的天數            $this->days = date("t",mktime(0,0,0,$this->month,1,$this->year));        }         //魔術方法用於列印整個日曆        function __toString(){            $out .='<table align="center">';            $out .=$this->chageDate();        //調用內部私人方法用於使用者自己設定日期            $out .=$this->weeksList();        //調用內部私人方法列印周列表            $out .=$this->daysList();         //調用內部私人方法列印日列表            $out .='</table>';             return $out;          //返回整個日曆輸需要的全部字串        }         //內部調用的私人方法,用於輸出周列表        private function weeksList(){            $week = array('日','一','二','三','四','五','六');            $out .= '<tr>';            for ($i = 0; $i<count($week); $i++)                  $out .= '<th class="fontb">'.$week[$i].'</th>';         //第一行以表格<th>輸出周列表            $out .= '</tr>';            return $out;          //返回周列表字串        }         //內部調用的私人方法,用於輸出周列表        private function daysList(){            $out .= '<tr>';            //輸出空格(當前一月第一天前面要空出來)            for ($j = 0; $j<$this->start_weekday; $j++)                  $out .= '<td> </td>';             //將當月的所有日期迴圈遍曆出來,如果是當前日期,為其設定深色背景            for ($k = 1; $k<=$this->days; $k++){                $j++;                 if ($k == date('d')){                     $out .= '<td class="fontb">'.$k.'</td>';                }else {                     $out .='<td>'.$k.'</td>';                 }                 if ($j%7 == 0)                   //每輸出7個日期,就換一行                     $out .= '</tr><tr>';        //輸出行結束和下一行開始            }             //遍曆完日期後,將後面用空格補齊            while ($j%7 !== 0){                                    $out .= '<td> </td>';                $j++;            }             $out .= '</tr>';            return $out;                      //返回當月日期列表        }         //內部調用的私人方法,用於處理當前年份的上一年需要的資料        private function prevYear($year,$month){            $year = $year-1;          //上一年是當前年減1             if($year < 1970)          //年份設定最小值是1970年              $year = 1970;             return "year={$year}&month={$month}";        //返回最終的年份和月份設定參數        }         //內部調用的私人方法,用於處理當前月份的上一月份需要的資料        private function prevMonth($year,$month){             if ($month == 1){                $year = $year-1;          //上一年是當前年減1                 if($year < 1970)          //年份設定最小值是1970年                    $year =1970;                $month = 12;           //如果是1月,上一月就是上一年的最後一月            }else {                    $month--;              //上一月份是當前月減1            }            return "year={$year}&month={$month}";        //返回最終的年份和月份設定參數        }         //內部調用的私人方法,用於處理當前年份的下一年份的資料        private function nextYear($year,$month){            $year = $year+1;          //下一年是當前年加1             if($year > 2038)          //年份設定最大值是2038年                    $year =2038;             return "year={$year}&month={$month}";        //返回最終的年份和月份設定參數        }         //內部調用的私人方法,用於處理當前月份的下一月份需要的資料        private function nextMonth($year,$month){             if ($month == 12){                $year++;                           if($year > 2038)         //年份設定最大值是2038年                    $year =2038;                $month = 1;           //如果是1月,上一月就是上一年的最後一月            }else {                    $month++;              //上一月份是當前月減1            }            return "year={$year}&month={$month}";        //返回最終的年份和月份設定參數        }         //內部調用的私人方法,用於使用者操作去調整年份和月份的設定        private function chageDate($url="index.php"){            $out .= '<tr>';            $out .= '<td><a href="'.$url.'?'.$this->prevYear($this->year,$this->month).'">'.'<<'.'</a></td>';            $out .= '<td><a href="'.$url.'?'.$this->prevMonth($this->year,$this->month).'">'.'<<'.'</a></td>';             $out .= '<td colspan="3">';            $out .= '<form>';            $out .= '<select name="year" onchange="window.location=\''.$url.            '?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';            for ($sy=1970; $sy<=2038;$sy++){                $selected = ($sy == $this->year) ? "selected" : "";                $out .= '<option '.$selected.' value="'.$sy.'">'.$sy.'</option>';            }            $out .= '</select>';            $out .= '<select name="month" onchange="window.location=\''.$url.            '?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';            for ($sm=1; $sm<=12;$sm++){                $selected1 = ($sm == $this->month) ? "selected" : "";                $out .= '<option '.$selected1.' value="'.$sm.'">'.$sm.'</option>';            }            $out .= '</select>';            $out .= '</form>';            $out .= '</td>';             $out .= '<td><a href="'.$url.'?'.$this->nextYear($this->year,$this->month).'">'.'>>'.'</a></td>';            $out .= '<td><a href="'.$url.'?'.$this->nextMonth($this->year,$this->month).'">'.'>>'.'</a></td>';            $out .= '</tr>';            return $out;                //返回日期表單        }    }?>

本例將一個行事曆程式按功能拆分(周列表部分、日期列表部分、設定日期部分,以及上一年、下一年、上一月和下一月的設定部分)並封裝在一個日曆類中。有了日曆類,我們還需要再編寫一個主程式去載入並輸出日曆,在主程式中還需要先設定一下日曆輸出的樣式,代碼如下所示:

<html>    <head>        <title>恩聰PHP日曆樣本</title>        <style>            table {border:1px solid #050;}            .fontb {color:white; background:blue;}            th{width:30px;}            td,th{height:30px;text-align:center;}            form{margin:0px; padding:0px;}        </style>    </head>    <body>        <?php        require 'calendar.class.php';        echo new calendar;        ?>    </body></html>

運行結果如圖所示,預設顯示當前系統日期。可以通過單擊“>>”按鈕設定下一年份,但設定的最大年份為2038年。也可以通過單擊“<<”按鈕設定上一年份,但設定的最小年份為1970年。還可以通過單擊“<”各“>”按鈕設定上一個和下一個月份,如果當月為12月,則設定的下一個月份就為次年的1月,如果當月為1月,則設定上一個月份就為上一年的12月。如果需要快速定位到指定的年份和月份,還可通過下拉式清單進行設定。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.