国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

一個比較不錯的PHP日歷類分享

2020-03-22 18:01:58
字體:
來源:轉載
供稿:網友
說到對時期和時間的處理,就一定要介紹一下日歷程序的編寫。但一提起編寫日歷,大多數人都會認為日歷的作用只是為了在頁上顯示當前的日期,其實日歷在我們的開發中有更重要的作用。例如我們開發一個“記事本”就需要通過日歷設定日期,還有一些系統中需要按日期去排任務,也需要日歷,等等。本例涉及的日期和時間函數并不是很多,都是前面介紹的內容,主要是通過一個日歷類的編寫,鞏固一下前面介紹過的html' target='_blank'>面向對象的語法知識,以及時間函數應用,最主要的是可以提升初學者的思維邏輯和程序設計能力。將日歷類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 '.$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 '.$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; //上一年是當前年減1if($year 1970) //年份設置最小值是1970年$year = 1970;return "year={$year}&month={$month}"; //返回最終的年份和月份設置參數}//內部調用的私有方法,用于處理當前月份的上一月份需要的數據private function prevMonth($year,$month){if ($month == 1){$year = $year-1; //上一年是當前年減1if($year 1970) //年份設置最小值是1970年$year =1970;$month = 12; //如果是1月,上一月就是上一年的最后一月}else {$month--; //上一月份是當前月減1}return "year={$year}&month={$month}"; //返回最終的年份和月份設置參數}//內部調用的私有方法,用于處理當前年份的下一年份的數據private function nextYear($year,$month){$year = $year+1; //下一年是當前年加1if($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 $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 phprequire 'calendar.class.php';echo new calendar; /body /html 運行結果如圖所示,默認顯示當前系統日期。可以通過單擊“ ”按鈕設置下一年份,但設置的最大年份為2038年。也可以通過單擊“ ”按鈕設置上一年份,但設置的最小年份為1970年。還可以通過單擊“ ”各“ ”按鈕設置上一個和下一個月份,如果當月為12月,則設置的下一個月份就為次年的1月,如果當月為1月,則設置上一個月份就為上一年的12月。如果需要快速定位到指定的年份和月份,還可通過下拉列表進行設置。PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安丘市| 南和县| 孟津县| 永川市| 安多县| 乡城县| 泽州县| 浦东新区| 景宁| 汨罗市| 唐河县| 萍乡市| 文登市| 万盛区| 郸城县| 石嘴山市| 峨眉山市| 绥宁县| 惠水县| 大丰市| 威远县| 伊川县| 永城市| 泸水县| 潮安县| 宜君县| 家居| 沛县| 南陵县| 固阳县| 禄劝| 泾川县| 龙川县| 宜黄县| 津南区| 济宁市| 筠连县| 布尔津县| 宁蒗| 山西省| 临江市|