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执行速度全攻略(上)
Oct 09 PHP
一个简单的php实现的MySQL数据浏览器
Mar 11 PHP
Discuz 模板语句分析及知识技巧
Aug 21 PHP
php实现mysql同步的实现方法
Oct 21 PHP
php实现首页链接查询 友情链接检查的代码
Jan 05 PHP
php将textarea数据提交到mysql出现很多空格的解决方法
Dec 19 PHP
php使用类继承解决代码重复的问题
Feb 11 PHP
php实现求相对时间函数
Jun 15 PHP
CodeIgniter配置之routes.php用法实例分析
Jan 19 PHP
Yii2实现同时搜索多个字段的方法
Aug 10 PHP
php+ajax实现仿百度查询下拉内容功能示例
Oct 20 PHP
php中加密解密DES类的简单使用方法示例
Mar 26 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提示undefined index的几种解决方法
2012/05/21 PHP
php生成扇形比例图实例
2013/11/06 PHP
Yii核心组件AssetManager原理分析
2014/12/02 PHP
一文掌握PHP Xdebug 本地与远程调试(小结)
2019/04/23 PHP
javascript学习网址备忘
2007/05/29 Javascript
JavaScript中的onerror事件概述及使用
2013/04/01 Javascript
js 阻止子元素响应父元素的onmouseout事件具体实现
2013/12/23 Javascript
javascript数组去重方法终极总结
2014/06/05 Javascript
javascript浏览器兼容教程之事件处理
2014/06/09 Javascript
用js判断是否为360浏览器的实现代码
2015/01/15 Javascript
javascript解析xml实现省市县三级联动的方法
2015/07/25 Javascript
JavaScript 模块的循环加载实现方法
2015/12/13 Javascript
将List对象列表转换成JSON格式的类实现方法
2016/07/04 Javascript
jquery-mobile表单的创建方法详解
2016/11/23 Javascript
详解Angular CLI + Electron 开发环境搭建
2017/07/20 Javascript
vue实现移动端图片裁剪上传功能
2020/08/18 Javascript
vue2.0+vuex+localStorage代办事项应用实现详解
2018/05/31 Javascript
vue 表单之通过v-model绑定单选按钮radio
2019/05/13 Javascript
[01:58]DOTA2上海特级锦标赛现场采访:RTZ这个ID到底好不好
2016/03/25 DOTA
Python多线程实例教程
2014/09/06 Python
Python设计模式中单例模式的实现及在Tornado中的应用
2016/03/02 Python
浅析Python基础-流程控制
2016/03/18 Python
python操作 hbase 数据的方法
2016/12/18 Python
python实现数独游戏 java简单实现数独游戏
2018/03/30 Python
python3 面向对象__类的内置属性与方法的实例代码
2018/11/09 Python
python 二维数组90度旋转的方法
2019/01/28 Python
泰国排名第一的家居用品中心:HomePro
2020/11/18 全球购物
PHP面试题附答案
2015/11/28 面试题
父亲生日宴会答谢词
2014/01/10 职场文书
迎接领导欢迎词
2014/01/11 职场文书
大学生的自我鉴定范文
2014/01/21 职场文书
音乐器材管理制度
2014/01/31 职场文书
商场消防演习方案
2014/02/12 职场文书
十佳标兵事迹材料
2014/08/18 职场文书
2015年大学班级工作总结
2015/04/28 职场文书
2015年文明创建工作总结
2015/04/30 职场文书