用php做个简单的日历详解编程语言

存档:

index.php

 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

  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 ?>

结果如下:

用php做个简单的日历详解编程语言

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/11937.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论