溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用PHP怎么編寫一個日歷類

發布時間:2020-12-18 14:41:31 來源:億速云 閱讀:171 作者:Leah 欄目:開發技術

本篇文章給大家分享的是有關利用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>&nbsp;</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>&nbsp;</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>

以上就是利用PHP怎么編寫一個日歷類,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女