(php)实现万年历

 1  <?php
 2 //修改页面编码
 3 header("content-type:text/html;charset=utf-8");
 4 
 5 //获取当前年
 6 $year=$_GET['y']?$_GET['y']:date('Y');
 7 
 8 //获取当年月
 9 $month=$_GET['m']?$_GET['m']:date('m');
10 
11 //获取当前月多少天
12 $days=date('t',strtotime("{$year}-{$month}-1"));
13 
14 //当前一号周几
15 $week=date('w',strtotime("{$year}-{$month}-1"));
16 
17 //居中
18 echo "<center>";
19 
20 //计算上个月
21 if($month==1)
22 {
23     $prevyear=$year-1;
24     $prevmonth=12;
25 }
26 else
27 {
28     $prevyear=$year;
29     $prevmonth=$month-1;
30 }
31 
32 //计算下个月
33 if($month==12)
34 {
35     $nextyear=$year+1;
36     $nextmonth=1;
37 }
38 else
39 { 
40     $nextyear=$year;
41     $nextmonth=$month+1;
42 }
43 
44 //输出表头
45 echo " <h2><a href='万年历.php?y={$prevyear}&m={$prevmonth}'>上一月</a>|{$year}年{$month}月|<a  href='万年历.php?y={$nextyear}&m={$nextmonth}'>下一月</a></h2>";
46 
47 //输出日期表格
48 echo "<table width='700px' border='1px'>";
49 echo "<tr>";
50 echo "<th>周日</th>";
51 echo "<th>周一</th>";
52 echo "<th>周二</th>";
53 echo "<th>周三</th>";
54 echo "<th>周四</th>";
55 echo "<th>周五</th>";
56 echo "<th>周六</th>";
57 echo "</tr>";
58 
59 //铺表格
60 for ($i=1-$week; $i <=$days ;)
61      { 
62         echo "<tr>";
63         for ($j=0; $j < 7; $j++)
64          { 
65             if ($i>$days || $i<1) 
66             {
67                 echo "<td>&nbsp;</td>";
68             }
69             else
70             {
71                  echo "<td>{$i}</td>";
72             }
73             $i++;
74         }
75         echo "</tr>";
76     }
77 echo "</table>";
78 echo "</center>";
79 
80 ?>

 

posted @ 2018-08-10 15:49  Connaught_yu  阅读(595)  评论(0编辑  收藏  举报