Share with you a simple perpetual calendar production process.
Basic requirements:
1. Obtain date
2. Get the date of a given date
3. Get the day of the week for a given date
4. Get the number of days in the month
5. Get the previous month and next month
Post a rendering first, the style is rather ugly, don’t spray it if you don’t like it.
php code:
<?php //修改字符编码 header("content-type:text/html;charset=utf-8"); //外部样式链接 echo "<link rel='stylesheet' type='text/css' href='calendar.css'/>"; //获取当前年 $year=$_GET['Y']?$_GET['Y']:date('Y'); //获取当前月 $month=$_GET['m']?$_GET['m']:date('m'); //获取当月有多少天 $days=date('t',strtotime("{$year}-{$month}-1")); //当前是周几 $week=date('w',strtotime("{$year}-{$month}-1")); //内容居中显示 echo "<center>"; //打印表头 echo "<h1>{$year}年{$month}月</h1>"; //打印日期表格 echo "<table>"; //打印星期 echo "<tr>"; echo "<th>周日</th>"; echo "<th>周一</th>"; echo "<th>周二</th>"; echo "<th>周三</th>"; echo "<th>周四</th>"; echo "<th>周五</th>"; echo "<th>周六</th>"; echo "</tr>"; //打印几号 for($i=1-$week;$i<=$days;){ echo "<tr>"; for($j=0;$j<7;$j++){ if($i>$days||$i<1){ echo "<td> </td>"; }else{ echo "<td>$i</td>"; } $i++; } echo "</tr>"; } echo "</table>"; //上一月和下一月算法 if($month==1){ $prevyear=$year-1; $prevmonth=12; }else{ $prevyear=$year; $prevmonth=$month-1; } if($month==12){ $nextyear=$year+1; $nextmonth=1; }else{ $nextyear=$year; $nextmonth=$month+1; } //上一月和下一月的超链接 echo "<h2>上一月&下一月</h2>"; echo "</center>"; ?>
table{ width:500px; height:300px; border:red dashed 1px; background:#ff00ff; } tr{ text-align:center; } td{ border:gray dotted 1px; } h1{ font-style:italic; font-size:50px; font-family:'宋体'; } h2 a{ font-style:normal; font-size:40px; font-family:'黑体'; color:purple; } /*组合选择器*/ tr,td,th{ font-size:20px; background:gray; }
1. The timestamp calculated in strtotime() should be in a complete format. It is useless to put a separate year or month in it.
2. The condition in the if statement is not assignment, but equal! ! ! , you need to write two ==. This place is so easily overlooked.
3. The originally printed date always corresponds to Sunday and the 1st. However, if the month is different, the corresponding relationship between the date and the week will also change. Therefore, by changing $i-$week. in the for loop, all the dates of the current month can be moved back a certain time to achieve a perfect correspondence between the date and the week.
4. When implementing the functions of the previous month and the next month, bring in several variables as parameters, and then cooperate with a certain algorithm to get it done. Specifically, just look at the code. No matter how good the text is, it is not as effective as reading two lines of code.