简单实用PHP日历程序代码

日历程序代码我们一般会使用一些js插件来实现了,但是像博客这种日志分类我们会使用php程序来实现,下面php粉丝网就来为你介绍一下吧.

PHP日历程序,功能都是大众化的,可以下拉切换年月,上一年下一月下一年上一月,太另类的没去写,主要的写出来了,扩展起来就方便多了,标题为什么要叫精美呢,是因自已感觉界面还过得去,哈哈,让大家见笑了,不足之处还请指出.

php日历核心代码如下:

  1. <?php
  2. //日历类
  3. class calendar {
  4. //当前的年
  5. private $year;
  6. //当前的月
  7. private $month;
  8. //一个月中第一天是星期几
  9. private $start_weekday;
  10. //当前月的天数
  11. private $days;
  12. //最大数与最小年数,最大与最小月数
  13. private $yearMonth = array(2080, 1900, 12, 1);
  14. //构造函数
  15. function __construct() {
  16. if (isset($_GET['year'])) {
  17. $this->year = $_GET['year'];
  18. }
  19. if (isset($_GET['month'])) {
  20. $this->month = $_GET['month'];
  21. }
  22. $this->pnYm($this->year, $this->month);
  23. $this->days = date('t', mktime(0, 0, 0, $this->month, 1, $this->year));
  24. $this->start_weekday = date('w', mktime(0, 0, 0, $this->month, 1, $this->year));
  25. $this->style();
  26. }
  27. //输出
  28. private function style() {
  29. echo '<table >';
  30. $this->weeklist();
  31. $this->daylist();
  32. echo '<table>';
  33. }
  34. //年月参数判断
  35. private function ymCheck($year, $month) {
  36. if (!is_numeric($year)) {
  37. $year = date('Y');
  38. }
  39. if (!is_numeric($month)) {
  40. $month = date('m');
  41. }
  42. if ($month < $this->yearMonth[3]) {
  43. $month = $this->yearMonth[2];
  44. $year -= 1;
  45. }
  46. if ($month > $this->yearMonth[2]) {
  47. $month = $this->yearMonth[3];
  48. $year = intval($year) + 1;
  49. }
  50. $year = $year < $this->yearMonth[1] ? $this->yearMonth[1] : $year;
  51. $year = $year > $this->yearMonth[0] ? $this->yearMonth[0] : $year;
  52. return array($year, $month);
  53. }
  54. //上一年、下一年、上一月、下一月
  55. private function pnYm($year, $month) {
  56. $ym = $this->ymCheck($year, $month);
  57. $this->year = $ym[0];
  58. $this->month = $ym[1];
  59. }
  60. //weeklist周列表
  61. private function weeklist() {
  62. $week = array('日','一','二','三','四','五','六');
  63. echo '<tr>';
  64. foreach ($week as $val) {
  65. echo '<th>'.$val.'</th>';
  66. }
  67. echo '</tr>';
  68. }
  69. //daylist天列表
  70. private function daylist() {
  71. //年月日导航
  72. echo '<tr>';
  73. echo '<td><a title="上一年" href="?year='.($this->year-1).'&month='.$this->month.'"><<</a></td>';
  74. echo '<td><a title="上一月" href="?year='.$this->year.'&month='.($this->month-1).'"><</a></td>';
  75. echo '<td colspan="3">';
  76. echo '<form action="?" method="get" >';
  77. echo '<select name="year" onchange="formaction()">';
  78. for ($i = $this->yearMonth[1]; $i <= $this->yearMonth[0]; $i++) {
  79. if ($i == $this->year) {
  80. echo '<option value="'.$i.'" selected="selected">'.$i.'年</option>';
  81. }else {
  82. echo '<option value="'.$i.'">'.$i.'年</option>';
  83. }
  84. }
  85. echo '</select>';
  86. echo '<select name="month" onchange="formaction()">';
  87. for ($i = $this->yearMonth[3]; $i <= $this->yearMonth[2]; $i++) {
  88. if ($i == $this->month) {
  89. echo '<option value="'.$i.'" selected="selected">'.$i.'月</option>'; //开源软件:phpfensi.com
  90. }else {
  91. echo '<option value="'.$i.'">'.$i.'月</option>';
  92. }
  93. }
  94. echo '</select></form></td>';
  95. echo '<td><a title="下一月" href="?year='.$this->year.'&month='.($this->month+1).'">></a></td>';
  96. echo '<td><a title="下一年" href="?year='.($this->year+1).'&month='.$this->month.'">>></a></td>';
  97. echo '</tr>';
  98. echo '<tr>';
  99. //输出空格(当前一个月第一天前面要空出来的)
  100. for($i = 0; $i < $this->start_weekday; $i++) {
  101. echo '<td>&nbsp;</td>';
  102. }
  103. for ($k = 1; $k <= $this->days; $k++) {
  104. $i++;
  105. if ($k == date('d')) {
  106. echo '<td>'.$k.'</td>';
  107. }else {
  108. echo '<td>'.$k.'</td>';
  109. }
  110. if ($i % 7 == 0) {
  111. if ($k != $this->days) {
  112. echo '</tr><tr>';
  113. }
  114. }
  115. }
  116. echo '</tr>';
  117. }
  118. }
  119. ?>

html+css代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>PHP日历程序</title>
  6. <style>
  7. #calendar { width:auto; margin:0 auto; margin-top:100px; border:0; border-collapse:collapse; box-shadow:0px 0px 4px #ddd; font-size:12px; text-align:center; font-family:"微软雅黑"; color:#333; border:solid 1px #c5e2ff; }
  8. #calendar tr { width:auto; height:34px; line-height:34px; }
  9. #calendar tr th { width:44px; background:#c5e2ff; }
  10. #calendar tr td { background:#fff; }
  11. #calendar tr td.tdbg { background:#c5e2ff; }
  12. #calendar tr td:hover { background:#FFC; }
  13. #calendar tr td a { text-decoration:none; color:#f50; font-weight:900; }
  14. #calendar select { width:auto; border:solid 1px #c5c5c5; padding:2px 0 2px 0; background:#fff; float:left; margin-left:5px; }
  15. </style>
  16. <script>
  17. function formaction() {
  18. var form = document.getElementById('form');
  19. form.submit();
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <?php
  25. require 'init.php';
  26. $calendar = new calendar();
  27. ?>
  28. </body>
  29. </html>