PHP实现简单日历类编写

这篇文章主要为大家详细介绍了PHP实现简单日历类编写,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

用PHP实现日历类的编写,供大家参考,具体内容如下

calendar.class.php

  1. <?php
  2. /*
  3. * 创建一个日历类
  4. *
  5. *
  6. */
  7. //修改默认时区
  8. date_default_timezone_set("PRC");
  9. class Calendar {
  10. private $year;
  11. private $month;
  12. private $day; //当月总天数
  13. private $first_week; //每月的第一天是星期几
  14. //构造函数
  15. function __construct() {
  16. $this->year = isset($_GET['year'])?$_GET['year']:date("Y");
  17. $this->month = isset($_GET["month"])?$_GET["month"]:date("m");
  18. $this->first_week = date("w", mktime(0, 0 ,0, $this->month, 1, $this->year));
  19. $this->day = date("t", mktime(0, 0 ,0, $this->month, 1, $this->year));
  20. }
  21. function showCalendar() {
  22. // echo $this->year."年".$this->month."月".$this->first_week."天".$this->day;
  23. echo "<table align='center'>"; //用表格输出
  24. $this->chageDate("index.php"); //用于用户调整年月份
  25. $this->weekList();//显示星期
  26. $this->dayList(); //显示天数
  27. echo "</table>";
  28. }
  29. //1、显示星期
  30. private function weekList() {
  31. $week = array("日","一","二","三","四","五","六");
  32. echo "<tr>";
  33. for ($i = 0; $i < count($week); $i++) {
  34. echo "<th>".$week[$i]."</th>";
  35. }
  36. echo "</tr>";
  37. }
  38. //2.显示天数
  39. private function dayList() {
  40. $color = "#2ca50c";
  41. echo "<tr>";
  42. for ($i = 0; $i < $this->first_week; $i++) { //输出空格,弥补当前月空缺部分
  43. echo "<td bgcolor='#2ca50c'> </td>";
  44. }
  45. for ($k = 1; $i <= $this->day; $k++) {
  46. $i++;
  47. if ($k == date("d")) echo "<td >".$k."</td>"; //是今天,加效果
  48. else echo "<td bgcolor=$color>".$k."</td>";
  49. if ($i % 7 == 0) {
  50. echo "</tr><tr>"; //每7天一次换行
  51. if ($i % 2 == 0) $color = "#2ca50c";
  52. else $color = "#9ddb27"; //实现各行换色的效果
  53. }
  54. }
  55. while ($i % 7 != 0) { //将剩余的空格补完
  56. echo "<td bgcolor='#2ca50c'> </td>";
  57. $i++;
  58. }
  59. echo "</tr>";
  60. }
  61. //3、用于用户调整天数
  62. private function chageDate($url="index.php") {
  63. echo "<tr>";
  64. echo "<caption><h1>".$this->year."年".$this->month."月</h1></caption>";
  65. echo "</tr>";
  66. echo "<tr>";
  67. echo "<td>"."<a href='?".$this->prevYear($this->year,$this->month)."'>"."<"."</a>";
  68. echo "<td>"."<a href='?".$this->prevMonth($this->year,$this->month)."'>"."<<"."</a>";
  69. echo "<td colspan='3'>";
  70. echo '<select οnchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
  71. for ($year = 2038; $year >= 1970; $year--) {
  72. $selected = ($year == $this->year)?"selected":"";
  73. echo '<option '.$selected. ' value="'.$year.'">'.$year.'</option>';
  74. //echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
  75. }
  76. echo "</select>";
  77. echo '<select name="month" οnchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
  78. for($month=1;$month <= 12;$month++){
  79. $selected1 = ($month == $this->month) ? "selected" : "";
  80. echo '<option '.$selected1.' value="'.$month.'">'.$month.'</option>';
  81. }
  82. echo '</select>';
  83. echo "</td>";
  84. echo "<td>"."<a href='?".$this->nextMonth($this->year,$this->month)."'>".">>"."</a>";
  85. echo "<td>"."<a href='?".$this->nextYear($this->year,$this->month)."'>".">"."</a>";
  86. echo "</tr>";
  87. }
  88. private function prevYear($year, $month) { //获取上一年的数据
  89. $year--;
  90. if ($year < 1970) $year = 1970;
  91. return "year={$year}&month={$month}";
  92. }
  93. private function prevMonth($year, $month) {
  94. if ($month == 1) {
  95. $year--;
  96. if ($year < 1970) $year = 1970;
  97. $month = 12;
  98. }else $month--;
  99. return "year={$year}&month={$month}";
  100. }
  101. private function nextYear($year, $month) { //获取上一年的数据
  102. $year++;
  103. if ($year > 2038) $year = 2038;
  104. return "year={$year}&month={$month}";
  105. }
  106. private function nextMonth($year, $month) {
  107. if ($month == 12) {
  108. $year++;
  109. if ($year > 2038) $year = 2038;
  110. $month = 1;
  111. }else $month++;
  112. return "year={$year}&month={$month}";
  113. }
  114. }

主页 index.php

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>日历显示</title>
  6. <style>
  7. table {
  8. border:1px solid #050;
  9. margin: 100px auto;
  10. }
  11. th {
  12. width: 30px;
  13. background-color: #0CC;
  14. color: #fff;
  15. height: 30px;
  16. font-size: 20px;
  17. }
  18. #nowd {
  19. color: yellow;
  20. background: #F00;
  21. }
  22. td {
  23. width: 30px;
  24. text-align: center;
  25. height: 25px;
  26. color: #fff;
  27. }
  28. a {
  29. display: block;
  30. width: 35px;
  31. height: 35px;
  32. background: #0F9;
  33. text-decoration: none;
  34. text-align: center;
  35. line-height: 35px;
  36. }
  37. a:hover {
  38. background: #CF0;
  39. color: #fff;
  40. font-size: 20px;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <?php
  46. include "calendar.class.php";
  47. $ca = new Calendar();
  48. $ca->showCalendar();
  49. ?>
  50. </body>
  51. </html>

PHP实现简单日历类编写