php如何实现万年历的开发(每日一课真是非常有效率)
php如何实现万年历的开发(每日一课真是非常有效率)
一、总结
一句话总结:
1、判断每月有多少天:
通过data函数来判断,$days=date('t',$firstday);
2、判断每月的第一天是星期几:
还是 通过data函数来判断,$week=date('w',$firstday);
3、如何获取某年某月某日对应的时间戳:
通过strtotime函数来得到,$firstday=strtotime("{$year}-{$month}-1");
4、如何实现上一月下一月操作:
get方式传递参数,把年和月作为参数传过去。
1、如何判断每月有多少天?
解答:通过data函数来判断,$days=date('t',$firstday);。
2、如何判断每月的第一天是星期几?
解答:还是 通过data函数来判断,$week=date('w',$firstday);。
3、如何获取某年某月某日对应的时间戳?
解答:通过strtotime函数来得到,$firstday=strtotime("{$year}-{$month}-1");。
4、php万年历脚本如何实现上一月下一月操作?
解答:get方式传递参数,把年和月作为参数传过去。
5、html中的php代码应该如何放置?
解答:若非逼不得已插入html代码中,可以找一块区域放php代码。
6、php如何获取get方式传过来的参数,比如y?
解答:$_GET['y']。
7、php代码如何内嵌入html中?
解答:正常的php代码的格式即可,<?php ?>,<h2>万年历-<?php echo $year ?>年<?php echo $month ?>月</h2>。
8、php万年历如何在每月1号的前面把该空出来的格子空出来?
解答:把他们依次看成0,-1,-2,-3等等,然后i从(1-$week)开始取$i=(1-$week);,然后那些小于1对应的格子td里面就输出空格就好。
9、如何get方式传参数?
解答:url?y=3&m=3。
10、php如何实现动态传参数?
解答:加php代码<?php ?>即可,然后echo出参数,<a href="datenew.php?y=<?php echo $prevyear ?>&m=<?php echo $prevmonth ?>">上一月</a>。
二、php实现万年历的开发
截图
代码
1 <?php 2 //当前年 3 $year=$_GET['y']?$_GET['y']:date('Y'); 4 5 //当前月 6 $month=$_GET['m']?$_GET['m']:date('m'); 7 8 9 //当前月1号的时间戳 10 $firstday=strtotime("{$year}-{$month}-1"); 11 12 //当前月天数 13 $days=date('t',$firstday); 14 15 //当前月1号是周几 16 $week=date('w',$firstday); 17 18 //下一年和下一月 19 $nextyear=$year; 20 $nextmonth=$month+1; 21 if($nextmonth>12){ 22 $nextyear=$year+1; 23 $nextmonth=1; 24 } 25 26 //上一年和上一月 27 $prevyear=$year; 28 $prevmonth=$month-1; 29 if($prevmonth<1){ 30 $prevyear=$year-1; 31 $prevmonth=12; 32 } 33 34 ?> 35 <!doctype html> 36 <html lang="en"> 37 <head> 38 <meta charset="UTF-8"> 39 <title>index</title> 40 <style> 41 *{ 42 font-family: 微软雅黑; 43 } 44 45 a{ 46 text-decoration: none; 47 color:#55f; 48 } 49 </style> 50 </head> 51 <body> 52 <center> 53 <h2>万年历-<?php echo $year ?>年<?php echo $month ?>月</h2> 54 <table border='1px' cellspacing='0' width='700px'> 55 <tr> 56 <th>周日</th> 57 <th>周一</th> 58 <th>周二</th> 59 <th>周三</th> 60 <th>周四</th> 61 <th>周五</th> 62 <th>周六</th> 63 </tr> 64 65 <?php 66 for($i=(1-$week);$i<=$days;){ 67 echo '<tr>'; 68 for($j=0;$j<7;$j++,$i++){ 69 if($i>$days || $i<1){ 70 echo "<td> </td>"; 71 }else{ 72 echo "<td>{$i}</td>"; 73 } 74 } 75 echo '</tr>'; 76 } 77 ?> 78 </table> 79 <h3> 80 <a href="datenew.php?y=<?php echo $prevyear ?>&m=<?php echo $prevmonth ?>">上一月</a> | 81 <a href="datenew.php?y=<?php echo $nextyear ?>&m=<?php echo $nextmonth ?>">下一月</a> 82 </h3> 83 </center> 84 </body> 85 </html>
三、测试题-简答题
1、如何判断每月有多少天?
解答:通过data函数来判断,$days=date('t',$firstday);。
2、如何判断每月的第一天是星期几?
解答:还是 通过data函数来判断,$week=date('w',$firstday);。
3、如何获取某年某月某日对应的时间戳?
解答:通过strtotime函数来得到,$firstday=strtotime("{$year}-{$month}-1");。
4、php万年历脚本如何实现上一月下一月操作?
解答:get方式传递参数,把年和月作为参数传过去。
5、html中的php代码应该如何放置?
解答:若非逼不得已插入html代码中,可以找一块区域放php代码。
6、php如何获取get方式传过来的参数,比如y?
解答:$_GET['y']。
7、php代码如何内嵌入html中?
解答:正常的php代码的格式即可,<?php ?>,<h2>万年历-<?php echo $year ?>年<?php echo $month ?>月</h2>。
8、php万年历如何在每月1号的前面把该空出来的格子空出来?
解答:把他们依次看成0,-1,-2,-3等等,然后i从(1-$week)开始取$i=(1-$week);,然后那些小于1对应的格子td里面就输出空格就好。
9、如何get方式传参数?
解答:url?y=3&m=3。
10、php如何实现动态传参数?
解答:加php代码<?php ?>即可,然后echo出参数,<a href="datenew.php?y=<?php echo $prevyear ?>&m=<?php echo $prevmonth ?>">上一月</a>。