用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 ?>
复制代码

结果如下:

 

posted @   Angel_Kitty  阅读(2477)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示
哥伦布
14°
14:09发布
哥伦布
14:09发布
14°
大雨
南风
3级
空气质量
相对湿度
93%
今天
中雨
14°/19°
周日
中雨
5°/19°
周一
1°/11°