php简单日历函数

这篇文章主要介绍了php简单日历函数,没有选择比较常见的用js生成的日历,而是用php输出了一个日历表格,感兴趣的小伙伴们可以参考一下,本文实例讲述了php实现的日历程序,分享给大家供大家参考,具体如下:

  1. <?php
  2. /*
  3. * php 输出日历程序
  4. */
  5. header("Content-type: text/html;charset=utf-8");
  6. $year=(!isset($_GET['year'])||$_GET['year']=="")?date("Y"):$_GET['year'];
  7. $month=(!isset($_GET['month'])||$_GET['month']=="")?date("n"):$_GET['month'];
  8. $curUrl=$_SERVER['PHP_SELF'];
  9. if($year<1971){
  10. echo "出错!";
  11. echo "<BR>";
  12. echo "<a href=",$curUrl,">Back</a>";
  13. exit();
  14. }
  15. ?>
  16. <table width="200" cellspacing="0" cellpadding="0" bordercolor="#E7E7E7" align="center">
  17. <tr align="center"><td colspan="2">
  18. <?php
  19. //<-------当月份超出1至12时的处理;开始------->
  20. if($month<1){
  21. $month=12;
  22. $year-=1;
  23. }
  24. if($month>12){
  25. $month=1;
  26. $year+=1;
  27. }
  28. //<-------当月份超出1至12时的处理;结束------->
  29. //<---------上一年,下一年,上月,下月的连接处理及输出;开始--------->
  30. echo "<a href=",$curUrl,"?year=".($year-1)."&month=".$month."><<</a>年<a href=",$curUrl,"?year=".($year+1)."&month=".$month.">>></a>";
  31. ?>
  32. </td><td colspan="3"><?php echo $year."年".$month."月";?>
  33. </td><td colspan="2">
  34. <?php
  35. echo "<a href=",$curUrl,"?month=".($month-1)."&year=".$year."><<</a>月<a href=",$curUrl,"?month=".($month+1)."&year=".$year.">>></a>";
  36. //<--------上一年,下一年,上月,下月的连接处理及输出;结束--------->
  37. ?></td></tr>
  38. <tr align=center><td><font color="red">日</font></td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr><tr>
  39. <?php
  40. $d=date("d");
  41. $FirstDay=date("w",mktime(0,0,0,$month,1,$year));//取得任何一个月的一号是星期几,用于计算一号是由表格的第几格开始
  42. $bgtoday=date("d");
  43. function font_color($month,$today,$year){//用于计算星期天的字体颜色
  44. $sunday=date("w",mktime(0,0,0,$month,$today,$year));
  45. if($sunday=="0"){
  46. $FontColor="red";
  47. }else{
  48. $FontColor="black";
  49. }
  50. return $FontColor;
  51. }
  52. function bgcolor($month,$bgtoday,$today_i,$year){//用于计算当日的背景颜色
  53. $show_today=date("d",mktime(0,0,0,$month,$today_i,$year));
  54. $sys_today=date("d",mktime(0,0,0,$month,$bgtoday,$year));
  55. if($show_today==$sys_today){
  56. $bgcolor="bgcolor=#6699FF";
  57. }else{
  58. $bgcolor="";
  59. }
  60. return $bgcolor;
  61. }
  62. function font_style($month,$today,$year){//用于计算星期天的字体风格
  63. $sunday=date("w",mktime(0,0,0,$month,$today,$year));
  64. if($sunday=="0"){
  65. $Font;
  66. }else{
  67. $Font;
  68. }
  69. return $FontStyle;
  70. }
  71. for($i=0;$i<=$FirstDay;$i++){//此for用于输出某个月的一号位置
  72. for($i;$i<$FirstDay;$i++){
  73. echo "<td align=center> </td>\n";
  74. }
  75. if($i==$FirstDay){
  76. echo "<td align=center ".bgcolor($month,$bgtoday,1,$year)."><font color=".font_color($month,1,$year).">".font_style($month,1,$year)."1</font></td>\n";
  77. if($FirstDay==6){//判断1号是否星期六
  78. echo "</tr>";
  79. }
  80. }
  81. }
  82. $countMonth=date("t",mktime(0,0,0,$month,1,$year));//某月的总天数
  83. for($i=2;$i<=$countMonth;$i++){//输出由1号定位,随后2号直至月尾的所有号数
  84. echo "<td align=center ".bgcolor($month,$bgtoday,$i,$year)."><font color=".font_color($month,$i,$year).">".font_style($month,$i,$year)."$i</font></td>\n";
  85. if(date("w",mktime(0,0,0,$month,$i,$year))==6){//判断该日是否星期六
  86. echo "</tr>\n";
  87. }
  88. }
  89. ?>
  90. </table>