PHP实现简单日历类编写


Posted in PHP onAugust 28, 2020

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

calendar.class.php

<?php
/*
* 创建一个日历类
*
*
*/
 //修改默认时区
 date_default_timezone_set("PRC");
 
 class Calendar {
  private $year;
 private $month;
 private $day; //当月总天数
 private $first_week; //每月的第一天是星期几
 
 //构造函数
 function __construct() {
  $this->year = isset($_GET['year'])?$_GET['year']:date("Y");
  $this->month = isset($_GET["month"])?$_GET["month"]:date("m");
  $this->first_week = date("w", mktime(0, 0 ,0, $this->month, 1, $this->year));
  $this->day = date("t", mktime(0, 0 ,0, $this->month, 1, $this->year));
 }
 function showCalendar() {
 //  echo $this->year."年".$this->month."月".$this->first_week."天".$this->day;
   echo "<table align='center'>"; //用表格输出
   $this->chageDate("index.php"); //用于用户调整年月份
  $this->weekList();//显示星期
  $this->dayList(); //显示天数
  
  echo "</table>";
 }
 //1、显示星期
 private function weekList() {
  $week = array("日","一","二","三","四","五","六");
  echo "<tr>";
   for ($i = 0; $i < count($week); $i++) {
   echo "<th>".$week[$i]."</th>";
  }
  echo "</tr>";
 }
 //2.显示天数
 private function dayList() {
  $color = "#2ca50c";
  echo "<tr>";
  for ($i = 0; $i < $this->first_week; $i++) { //输出空格,弥补当前月空缺部分
   echo "<td bgcolor='#2ca50c'> </td>";
  }
  for ($k = 1; $i <= $this->day; $k++) {
   $i++;
   if ($k == date("d")) echo "<td id='nowd'>".$k."</td>"; //是今天,加效果
   else echo "<td bgcolor=$color>".$k."</td>";
   if ($i % 7 == 0) {
   echo "</tr><tr>"; //每7天一次换行
   if ($i % 2 == 0) $color = "#2ca50c";
   else $color = "#9ddb27"; //实现各行换色的效果
   }
  }
  while ($i % 7 != 0) { //将剩余的空格补完
   echo "<td bgcolor='#2ca50c'> </td>";
  $i++; 
  }
  echo "</tr>";
 }
  
 //3、用于用户调整天数
 private function chageDate($url="index.php") {
  echo "<tr>";
   echo "<caption><h1>".$this->year."年".$this->month."月</h1></caption>"; 
  echo "</tr>";
  echo "<tr>";
  echo "<td>"."<a href='?".$this->prevYear($this->year,$this->month)."'>"."<"."</a>";
  echo "<td>"."<a href='?".$this->prevMonth($this->year,$this->month)."'>"."<<"."</a>";
  
  echo "<td colspan='3'>";
   echo '<select οnchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
    for ($year = 2038; $year >= 1970; $year--) {
    $selected = ($year == $this->year)?"selected":"";
    echo '<option '.$selected. ' value="'.$year.'">'.$year.'</option>';
    //echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
   }
   echo "</select>";
   
  echo '<select name="month" οnchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
  for($month=1;$month <= 12;$month++){
   $selected1 = ($month == $this->month) ? "selected" : "";
   echo '<option '.$selected1.' value="'.$month.'">'.$month.'</option>';
  }
  echo '</select>';
  echo "</td>";
  
  
  echo "<td>"."<a href='?".$this->nextMonth($this->year,$this->month)."'>".">>"."</a>";
  echo "<td>"."<a href='?".$this->nextYear($this->year,$this->month)."'>".">"."</a>";
  echo "</tr>";
 }
 
 private function prevYear($year, $month) { //获取上一年的数据
  $year--;
  if ($year < 1970) $year = 1970;
  return "year={$year}&month={$month}";
 }
 private function prevMonth($year, $month) {
  if ($month == 1) {
   $year--;
  if ($year < 1970) $year = 1970;
  $month = 12;
  }else $month--; 
  return "year={$year}&month={$month}";
 }
 private function nextYear($year, $month) { //获取上一年的数据
  $year++;
  if ($year > 2038) $year = 2038;
  return "year={$year}&month={$month}";
 }
 private function nextMonth($year, $month) {
  if ($month == 12) {
   $year++;
  if ($year > 2038) $year = 2038;
  $month = 1;
  }else $month++; 
  return "year={$year}&month={$month}";
 }
 }

主页 index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>日历显示</title>
<style>
 table {
 border:1px solid #050;
 margin: 100px auto;
 }
 th {
  width: 30px;
 background-color: #0CC;
 color: #fff;
 height: 30px;
 font-size: 20px;
 }
 #nowd {
  color: yellow;
 background: #F00;
 }
 td {
  width: 30px;
 text-align: center;
 
 height: 25px;
 color: #fff;
 }
 a {
 display: block;
 width: 35px;
 height: 35px;
 background: #0F9;
  text-decoration: none;
 text-align: center;
 line-height: 35px;
 }
 a:hover {
  background: #CF0;
 color: #fff;
 font-size: 20px;
 }
</style>
</head>
 
<body>
 <?php
 include "calendar.class.php";
 $ca = new Calendar();
 $ca->showCalendar();
 ?>
</body>
</html>

PHP实现简单日历类编写

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
PHP和Mysqlweb应用开发核心技术-第1部分 Php基础-2 php语言介绍
Jul 03 PHP
PHP中获取时间的下一周下个月的方法
Mar 18 PHP
PHP 正则表达式常用函数
Aug 17 PHP
PHP文件生成的图片无法使用CDN缓存的解决方法
Jun 20 PHP
PHP引用的调用方法分析
Apr 25 PHP
Yii2超好用的日期和时间组件(值得收藏)
May 05 PHP
对比PHP对MySQL的缓冲查询和无缓冲查询
Jul 01 PHP
PHP微信支付实例解析
Jul 22 PHP
全面解析PHP面向对象的三大特征
Jun 10 PHP
PHP设计模式之工厂模式详解
Oct 24 PHP
PHP设计模式之外观模式(Facade)入门与应用详解
Dec 13 PHP
PHP实现简易图形计算器
Aug 28 PHP
PHP实现文件上传与下载
Aug 28 #PHP
PHP实现计算器小功能
Aug 28 #PHP
PHP实现简易图形计算器
Aug 28 #PHP
PHP实现简单的计算器
Aug 28 #PHP
php实现简易计算器
Aug 28 #PHP
有关PHP 中 config.m4 的探索
Aug 26 #PHP
安装PHP扩展时解压官方 tgz 文件后没有configure文件无法进行配置编译的问题
Aug 26 #PHP
You might like
php下intval()和(int)转换使用与区别
2008/07/18 PHP
yiic命令时提示“php.exe”不是内部或外部命令的解决方法
2014/12/18 PHP
php设计模式之单例模式实例分析
2015/02/25 PHP
php str_getcsv把字符串解析为数组的实现方法
2017/04/05 PHP
PHP实现的堆排序算法详解
2017/08/17 PHP
php生成HTML文件的类方法
2019/10/11 PHP
jquery 实现checkbox全选,反选,全不选等功能代码(奇数)
2012/10/24 Javascript
js替换字符串的所有示例代码
2013/07/23 Javascript
js 中将多个逗号替换为一个逗号的代码
2014/06/07 Javascript
浅谈JS闭包中的循环绑定处理程序
2014/11/09 Javascript
js仿百度登录页实现拖动窗口效果
2016/03/11 Javascript
JavaScript利用闭包实现模块化
2017/01/13 Javascript
Angular 1.x个人使用的经验小结
2017/07/19 Javascript
使用node打造自己的命令行工具方法教程
2018/03/26 Javascript
jQuery实现动态添加和删除input框代码实例
2019/03/29 jQuery
JS实现checkbox互斥(单选)功能示例
2019/05/04 Javascript
如何阻止小程序遮罩层下方图层滚动
2019/09/05 Javascript
[02:38]DOTA2超级联赛专访Loda 认为IG世界最强
2013/05/27 DOTA
基于python 处理中文路径的终极解决方法
2018/04/12 Python
Python简单定义与使用二叉树示例
2018/05/11 Python
python调用webservice接口的实现
2019/07/12 Python
关于Tensorflow使用CPU报错的解决方式
2020/02/05 Python
python unichr函数知识点总结
2020/12/16 Python
Html5 localStorage入门教程
2018/04/26 HTML / CSS
介绍一下mysql的日期和时间函数
2013/03/28 面试题
班主任寄语大全
2014/04/04 职场文书
借款协议书范本
2014/04/22 职场文书
环保建议书200字
2014/05/14 职场文书
施工安全生产承诺书
2014/05/23 职场文书
教研处工作方案
2014/05/26 职场文书
国际贸易专业自荐信
2014/06/10 职场文书
学校四风问题对照检查材料思想汇报
2014/09/26 职场文书
2014个人年度工作总结范文
2014/12/24 职场文书
致运动员赞词
2015/07/22 职场文书
oracle覆盖导入dmp文件的2种方法
2021/05/21 Oracle
MySQL系列之十三 MySQL的复制
2021/07/02 MySQL