PHP实现简单的万年历

 1 <?php
 2     /***********************
 3     ***    功能:万年历     ***
 4     ***    时间:2015/05/23 ***
 5     ***********************/
 6     
 7     //1、获取日期信息年和月(默认为当前年和当前月)
 8     error_reporting(E_ALL && (~E_NOTICE));
 9     $year = $_GET["y"] ? $_GET["y"] : date("Y");
10     $mon = $_GET["m"] ? $_GET["m"] : date("m");
11     
12     //2、计算出当前月有多少天和本月1号是星期几
13     $day = date("t",mktime(0,0,0,$mon,1,$year));//获取对应的天数
14     $w = date("w",mktime(0,0,0,$mon,1,$year));//获取当月1号是星期几
15     
16     //3、输出日期的头部信息(标题和表头)
17     echo "<center>";
18     echo "<h1>{$year}年{$mon}月</h1>";
19     echo "<table width = '600' border = '1'>";
20     echo "<tr>";
21     echo "<th style = 'color : red'>星期日</th>";
22     echo "<th>星期一</th>";
23     echo "<th>星期二</th>";
24     echo "<th>星期三</th>";
25     echo "<th>星期四</th>";
26     echo "<th>星期五</th>";
27     echo "<th style = 'color : green'>星期六</th>";
28     echo "</tr>";
29     
30     
31     //4、循环遍历输出日期信息
32     $dd = 1;//定义一个循环的天数
33     while($dd <= $day){
34         echo "<tr>";
35         //输出一周的信息该输出日期的时候,或已经日期溢出时,输出的都是空单元格
36         for($i = 0;$i < 7;$i ++){
37             //当还没有到
38             if(($w > $i && $dd == 1) || $dd > $day){
39                 echo "<td>&nbsp;</td>";
40             }else{
41                 echo "<td>{$dd}</td>";
42                 $dd ++;
43             }
44             
45             
46             //若没有输出完日期dd信息
47             /* if($dd <= $day && ($w <= $i || $dd != 1)){
48                 echo "<td>{$dd}</td>";
49                 $dd ++;
50             }else{
51                 echo "<td>&nbsp;</td>";
52             } */
53         }
54         echo "</tr>";
55     }
56     
57     
58     echo "</table>";
59     
60     
61     //5、输出上一月和下一月的超级链接
62     //处理上一月和下一月的信息
63     $prey = $nexty = $year;
64     $prem = $nextm = $mon;
65     if($prem <= 1){
66         $prem = 12;
67         $prey --;
68     }else{
69         $prem --;
70     }
71     if($nextm >= 12){
72         $nextm = 1;
73         $nexty ++;
74     }else{
75         $nextm ++;
76     }
77     echo "<h4><a href = 'wannianli.php?y={$prey}&m={$prem}'>上一月</a>&nbsp;&nbsp;&nbsp;&nbsp;";
78     
79     echo "<a href = 'wannianli.php?y={$nexty}&m={$nextm}'>下一月</a></h4>";
80     
81     
82     echo "</center>";
原文地址:https://www.cnblogs.com/banshaohuan/p/4525357.html