1. 程式人生 > 實用技巧 >php日曆類

php日曆類

檔案calender.class.php:

<?php
/*
    file: calendar.class.php日曆類原檔案
    宣告一個日曆類,名稱為calenar,用來顯示一個可以設定日期的日曆
*/
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>&nbsp;</td>'; /*將當月的所有日期迴圈遍歷出來,如果是當前日期,為其設定深色背景*/ for($k = 1; $k <= $this->days; $k++){ $j ++; if($k == date('d') && $this->year == date("Y") && $this->month == date("m")) $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>&nbsp;</td>'; //使用空格去補 $j++; } $out .= '</tr>' ; $out = str_replace('<tr></tr>', '', $out); return $out ; //返回當月日期列表 } /*內部呼叫的私有方法,用於處理當前年份的上一年需要的資料 */ private function prevyear ($year, $month) { $year = $year - 1; //上一年是當前年減1 if($year < 1970) //如果設定的年份小於1970年 $year = 1970; //年份設定最小值是1970年 return "year={$year}&month={$month} "; //返回最終的年份和月份設定引數 } /*內部呼叫的私有方法,用於處理當前月份的上一月份的資料*/ private function prevMonth($year, $month) { if($month == 1){ //如果月份已經是1月 $year = $year - 1; //則上一個月就是上一年的最後一個月 if($year < 1970) //和前面一樣,上一年如果是1970年 $year = 1970; //最小年份數不能小於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; //最大年份不超過2038年 return "year={$year}&month={$month}"; //返回最終的年份和月份設定引數 } /*內部呼叫的私有方法,用於處理當前月份的下一個月份的資料*/ private function nextMonth($year, $month){ if($month == 12){ //如果已經是當年的最後一個月 $year++; //下一個月就是下一年的第一個月,讓年份加1 if($year > 2038) //如果設定的年份大於2038年 $year = 2038; //最大年份不能超過2038年 $month = 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>';

/*注意:下面有兩處bootstrap框架程式碼,如果不需要可刪除,class="btn btn-primary"*/
$out .= '<select name="year" class="btn btn-primary btn-left" 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" class="btn btn-primary btn-right" 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->nextMonth($this->year, $this->month) . '">' . '>' . '</a></td>'; $out .= '<td><a href="' . $url . '?'. $this->nextYear($this->year, $this->month) . '">' . '>>' . '</a></td>'; $out .= '</tr>'; return $out; //返回調整日期的表單 } }

測試呼叫的主程式index.php:

<!DOCTYPE html>
<html>
 <head>
        <title>日曆示例</title>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
 <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
 <script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
 <script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
        <style>
            /* 日曆類樣式 */
            table {
                border:1px solid #050;  /*給表格加一 個外邊框*/
                width: 100%;
                height: auto;
                background-color: #fffaf0
            }
            
            /*設定周列表的背景和字型顏色*/
            .fontb {
                color: white;
                background: #000080
            }
            
            /*設定單元格的寬度*/
            th {
                width: 30px
            }
            
            /*設定單元格的高度和欄位顯示位置*/
            td,th {
                height: 30px;
                text-align: center
            }
            
            /*清除表單原有的樣式*/
            form {
                margin: 0px;
                padding: 0px
            }
            
            td a{
                text-decoration: none
            }
            .btn-left{
                width: 5.2em
            }
            .btn-right{
                width: 3.4em
            }
        </style>
 </head>
<body>
 <div class="container">
<?php
    require "calendar.class.php";  //載入日曆類
    echo  new Calendar;               //.直接輸出日曆物件,自動呼叫魔術方法_ tostring()列印日曆
?>
 <div>
</body>
</html>

執行後的效果: