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中GET变量的使用
Oct 09 PHP
PHP Ajax实现页面无刷新发表评论
Jan 02 PHP
PHP连接局域网MYSQL数据库的简单实例
Aug 26 PHP
部署PHP项目应该注意的几点事项分享
Dec 20 PHP
php分页示例分享
Apr 30 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
Jun 25 PHP
php实现的ping端口函数实例
Nov 12 PHP
利用PHP fsockopen 模拟POST/GET传送数据的方法
Sep 22 PHP
Zend Framework教程之Zend_Form组件实现表单提交并显示错误提示的方法
Mar 21 PHP
php处理多图上传压缩代码功能
Jun 13 PHP
laravel框架中视图的基本使用方法分析
Nov 23 PHP
Laravel jwt 多表(多用户端)验证隔离的实现
Dec 18 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和ACCESS写聊天室(九)
2006/10/09 PHP
php读取3389的脚本
2014/05/06 PHP
ThinkPHP多语言支持与多模板支持概述
2014/08/22 PHP
Smarty模板简单配置与使用方法示例
2016/05/23 PHP
php中让人头疼的浮点数运算分析
2016/10/10 PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
2017/07/10 PHP
jQuery select控制插件
2009/08/17 Javascript
javascript禁用Tab键脚本实例
2013/11/22 Javascript
node.js中的path.join方法使用说明
2014/12/08 Javascript
JS使用正则表达式除去字符串中重复字符的方法
2015/11/05 Javascript
jQuery常用样式操作实例分析(获取、设置、追加、删除、判断等)
2016/09/08 Javascript
Js apply方法详解
2017/02/16 Javascript
原生javascript移动端滑动banner效果
2017/03/10 Javascript
JavaScript fetch接口案例解析
2018/08/30 Javascript
Vue 路由间跳转和新开窗口的方式(query、params)
2019/12/25 Javascript
javascript 使用sleep函数的常见方法详解
2020/04/26 Javascript
在Python中的Django框架中进行字符串翻译
2015/07/27 Python
Python脚本获取操作系统版本信息
2016/12/17 Python
Windows下安装python MySQLdb遇到的问题及解决方法
2017/03/16 Python
Python字符串格式化%s%d%f详解
2018/02/02 Python
详谈Python 窗体(tkinter)表格数据(Treeview)
2018/10/11 Python
CentOS7安装Python3的教程详解
2019/04/10 Python
国外平面设计第一市场:99designs
2016/10/25 全球购物
Emporio Armani腕表天猫官方旗舰店:乔治·阿玛尼为年轻人设计的副线品牌
2017/07/02 全球购物
JBL英国官网:JBL UK
2018/07/04 全球购物
be2台湾单身男女交友:全球网路婚姻介绍的领导品牌
2019/10/11 全球购物
师范生实习个人的自我评价
2013/09/28 职场文书
高等教育学专业自荐书
2014/06/17 职场文书
中秋客户感谢信
2015/01/22 职场文书
2015年六一儿童节活动总结
2015/02/11 职场文书
mybatis调用sqlserver存储过程返回结果集的方法
2021/05/08 SQL Server
浅谈Python魔法方法
2021/06/28 Java/Android
Python中文分词库jieba(结巴分词)详细使用介绍
2022/04/07 Python
Python使用BeautifulSoup4修改网页内容
2022/05/20 Python
Win11远程连接不上怎么办?Win11远程桌面用不了的解决方法
2022/08/05 数码科技
MySQL存储过程及语法详解
2022/08/05 MySQL