前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用php做个简单的日历

用php做个简单的日历

作者头像
Angel_Kitty
发布2018-04-08 16:50:17
2.2K0
发布2018-04-08 16:50:17
举报
文章被收录于专栏:小樱的经验随笔

存档:

index.php

代码语言:javascript
复制
 1 <html>
 2     <head>
 3         <title>日历</title>
 4         <style>
 5             table{border:1px solid #050;}
 6             .fontb{color:white;background:blue;}
 7             th{width:30px;}
 8             td,th{height:30px;text-align:center;}
 9             form{margin:0px;padding:0px;}
10         </style>
11     </head>
12     <body>
13         <?php
14             require "calendar.class.php";
15             echo new Calendar;
16         ?>
17     </body>
18 </html>

calendar.php

代码语言:javascript
复制
  1 <?php
  2     class Calendar{
  3         private $year;
  4         private $month;
  5         private $start_weekday;
  6         private $days;
  7         function __construct(){
  8             $this->year = isset($_GET["year"])?$_GET["year"]:date("Y");
  9             $this->month = isset($_GET["month"])?$_GET["month"]:date("m");
 10             $this->start_weekday = date("w",mktime(0,0,0,$this->month,1,$this->year));
 11             $this->days = date("t",mktime(0,0,0,$this->month,1,$this->year));
 12         }
 13         
 14         function __toString(){
 15             $out .='<table align="center">';
 16             $out .=$this->chageDate();
 17             $out .=$this->weeksList();
 18             $out .=$this->daysList();
 19             $out .='</table>';
 20             return $out;
 21         }
 22         
 23         private function weeksList(){
 24             $week = array('日','一','二','三','四','五','六');
 25             $out .= '<tr>';
 26             for($i=0;$i<count($week);$i++){
 27                 $out .= '<th class="fontb">'.$week[$i].'</th>'; 
 28             }
 29             $out .= '</tr>';
 30             return $out;
 31         }
 32         
 33         private function daysList(){
 34             $out .= '<tr>';
 35             for($j=0;$j<$this->start_weekday;$j++){
 36                 $out .= '<td>&nbsp;</td>';
 37             }
 38             for($k=1;$k<=$this->days;$k++){
 39                 $j++;
 40                 if($k==date('d')){
 41                     $out .= '<td class="fontb">'.$k.'</td>';
 42                 }
 43                 else{
 44                     $out .= '<td>'.$k.'</td>';
 45                 }
 46                 if($j%7==0){
 47                     $out .= '</tr><tr>';
 48                 }
 49             }
 50             while($j%7!=0){
 51                 $out .= '<td>&nbsp;</td>';
 52                 $j++;
 53             }
 54             $out .= '</tr>';
 55             return $out;
 56         }
 57         
 58         private function prevYear($year,$month){
 59             $year = $year-1;
 60             if($year<1970){
 61                 $year=1970;
 62             }
 63             return "year=($year)&month=($month)";
 64         }
 65         
 66         private function prevMonth($year,$month){
 67             if($month==1){
 68                 $year=$year-1;
 69                 if($year<1970){
 70                     $year=1970;
 71                 }
 72                 $month=12;
 73             }
 74             else{
 75                 $month--;
 76             }
 77             return "year=($year)&month=($month)";
 78         }
 79         
 80         private function nextYear($year,$month){
 81             $year=$year+1;
 82             if($year>2038){
 83                 $year=2038;
 84             }
 85             return "year=($year)&month=($month)";
 86         }
 87         
 88         private function nextMonth($year,$month){
 89             if($month==12){
 90                 $year++;
 91                 if($year>2038){
 92                     $year=2038;
 93                 }
 94                 $month=1;
 95             }
 96             else{
 97                 $month++;
 98             }
 99             return "year=($year)&month=($month)";
100         }
101         
102         private function chageDate($url="index.php"){
103             $out .= '<tr>';
104             $out .= '<td><a href="'.$url.'?'.$this->prevYear($this->year,$this->month).'">'.'<<'.'</a></td>';
105             $out .= '<td><a href="'.$url.'?'.$this->prevMonth($this->year,$this->month).'">'.'<'.'</a></td>';
106             $out .= '<td colspan="3">';
107             $out .= '<form>';
108             $out .= '<select name="year" onchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
109             for($sy=1970;$sy<=2038;$sy++){
110                 $selected=($sy==$this->year)?"selected":"";
111                 $out .='<option '.$selected.' value="'.$sy.'">'.$sy.'</option>';
112             }
113             $out .= '</select>';
114             $out .= '<select name="month" onchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
115             for($sm=1;$sm<=12;$sm++){
116                 $selected1=($sm==$this->month)?"selected":"";
117                 $out .= '<option '.$selected1.' value="'.$sm.'">'.$sm.'</option>';
118             }
119             $out .= '</select>';
120             $out .= '</form>';
121             $out .= '</td>';
122             $out .= '<td><a href="'.$url.'?'.$this->nextYear($this->year,$this->month).'">'.'>>'.'</a></td>';
123             $out .= '<td><a href="'.$url.'?'.$this->nextMonth($this->year,$this->month).'">'.'>'.'</a></td>';
124             $out .= '</tr>';
125             return $out;
126         }
127     }
128 ?>

结果如下:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-02-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档