设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间09 现在时间09 缓存数据 当你还没强大到一定程度,没必要到处抱怨你的处境,默默做好你该做的事情,等你变得足够好,自然会有配得上你的人,拉你进更好的圈子,到那时,你会站在新的高度,拥抱更美的风景。

当你还没强大到一定程度,没必要到处抱怨你的处境,默默做好你该做的事情,等你变得足够好,自然会有配得上你的人,拉你进更好的圈子,到那时,你会站在新的高度,拥抱更美的风景。

查看: 1137|回复: 0

PHP实现简单日历类编写

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:225
  • 打卡月天数:0
  • 打卡总奖励:3161
  • 最近打卡:2025-04-07 22:19:19
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
407
主题
371
精华
0
金钱
4345
积分
832
注册时间
2023-1-6
最后登录
2025-4-7

发表于 2023-2-12 20:45:21 | 显示全部楼层 |阅读模式
用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.   
  28.   echo "</table>";
  29. }
  30. //1、显示星期
  31. private function weekList() {
  32.   $week = array("日","一","二","三","四","五","六");
  33.   echo "<tr>";
  34.    for ($i = 0; $i < count($week); $i++) {
  35.    echo "<th>".$week[$i]."</th>";
  36.   }
  37.   echo "</tr>";
  38. }
  39. //2.显示天数
  40. private function dayList() {
  41.   $color = "#2ca50c";
  42.   echo "<tr>";
  43.   for ($i = 0; $i < $this->first_week; $i++) { //输出空格,弥补当前月空缺部分
  44.    echo "<td bgcolor='#2ca50c'> </td>";
  45.   }
  46.   for ($k = 1; $i <= $this->day; $k++) {
  47.    $i++;
  48.    if ($k == date("d")) echo "<td id='nowd'>".$k."</td>"; //是今天,加效果
  49.    else echo "<td bgcolor=$color>".$k."</td>";
  50.    if ($i % 7 == 0) {
  51.    echo "</tr><tr>"; //每7天一次换行
  52.    if ($i % 2 == 0) $color = "#2ca50c";
  53.    else $color = "#9ddb27"; //实现各行换色的效果
  54.    }
  55.   }
  56.   while ($i % 7 != 0) { //将剩余的空格补完
  57.    echo "<td bgcolor='#2ca50c'> </td>";
  58.   $i++;
  59.   }
  60.   echo "</tr>";
  61. }
  62.   
  63. //3、用于用户调整天数
  64. private function chageDate($url="index.php") {
  65.   echo "<tr>";
  66.    echo "<caption><h1>".$this->year."年".$this->month."月</h1></caption>";
  67.   echo "</tr>";
  68.   echo "<tr>";
  69.   echo "<td>"."<a href='?".$this->prevYear($this->year,$this->month)."'>"."<"."</a>";
  70.   echo "<td>"."<a href='?".$this->prevMonth($this->year,$this->month)."'>"."<<"."</a>";
  71.   
  72.   echo "<td colspan='3'>";
  73.    echo '<select οnchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
  74.     for ($year = 2038; $year >= 1970; $year--) {
  75.     $selected = ($year == $this->year)?"selected":"";
  76.     echo '<option '.$selected. ' value="'.$year.'">'.$year.'</option>';
  77.     //echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
  78.    }
  79.    echo "</select>";
  80.    
  81.   echo '<select name="month" οnchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
  82.   for($month=1;$month <= 12;$month++){
  83.    $selected1 = ($month == $this->month) ? "selected" : "";
  84.    echo '<option '.$selected1.' value="'.$month.'">'.$month.'</option>';
  85.   }
  86.   echo '</select>';
  87.   echo "</td>";
  88.   
  89.   
  90.   echo "<td>"."<a href='?".$this->nextMonth($this->year,$this->month)."'>".">>"."</a>";
  91.   echo "<td>"."<a href='?".$this->nextYear($this->year,$this->month)."'>".">"."</a>";
  92.   echo "</tr>";
  93. }

  94. private function prevYear($year, $month) { //获取上一年的数据
  95.   $year--;
  96.   if ($year < 1970) $year = 1970;
  97.   return "year={$year}&month={$month}";
  98. }
  99. private function prevMonth($year, $month) {
  100.   if ($month == 1) {
  101.    $year--;
  102.   if ($year < 1970) $year = 1970;
  103.   $month = 12;
  104.   }else $month--;
  105.   return "year={$year}&month={$month}";
  106. }
  107. private function nextYear($year, $month) { //获取上一年的数据
  108.   $year++;
  109.   if ($year > 2038) $year = 2038;
  110.   return "year={$year}&month={$month}";
  111. }
  112. private function nextMonth($year, $month) {
  113.   if ($month == 12) {
  114.    $year++;
  115.   if ($year > 2038) $year = 2038;
  116.   $month = 1;
  117.   }else $month++;
  118.   return "year={$year}&month={$month}";
  119. }
  120. }
复制代码
主页 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>
复制代码
214629vi4fcgf9f4f0kk9i.jpeg

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持晓枫资讯。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表