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动态实现表格跨行跨列实现代码
Nov 06 PHP
基于PHP异步执行的常用方式详解
Jun 03 PHP
基于PHP对XML的操作详解
Jun 07 PHP
PHP中使用memcache存储session的三种配置方法
Apr 05 PHP
php运行提示:Fatal error Allowed memory size内存不足的解决方法
Dec 17 PHP
最新版本PHP 7 vs HHVM 多角度比较
Feb 14 PHP
PHP请求Socket接口测试实例
Aug 12 PHP
Yii2中SqlDataProvider用法示例
Sep 22 PHP
在laravel中使用Symfony的Crawler组件分析HTML
Jun 19 PHP
PHP调用接口用post方法传送json数据的实例
May 31 PHP
详解PHP神奇又有用的Trait
Mar 25 PHP
PHP实现的文件浏览器功能简单示例
Sep 12 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
source.php查看源文件
2006/12/09 PHP
常用PHP框架功能对照表
2014/10/23 PHP
php实现编辑和保存文件的方法
2015/07/20 PHP
PHP实现数组array转换成xml的方法
2016/07/19 PHP
jQuery 连续列表实现代码
2009/12/21 Javascript
自制轻量级仿jQuery.boxy对话框插件代码
2010/10/26 Javascript
用XMLDOM和ADODB.Stream实现base64编码解码实现代码
2010/11/28 Javascript
有关于JS辅助函数inherit()的问题
2013/04/07 Javascript
jquery each的几种常用的使用方法示例
2014/01/21 Javascript
javascript实例--教你实现扑克牌洗牌功能
2014/05/15 Javascript
jquery中$(#form :input)与$(#form input)的区别
2014/08/18 Javascript
Javascript判断文件是否存在(客户端/服务器端)
2014/09/16 Javascript
jQuery中queue()方法用法实例
2014/12/29 Javascript
js使用DOM设置单选按钮、复选框及下拉菜单的方法
2015/01/20 Javascript
js显示文本框提示文字的方法
2015/05/07 Javascript
jquery模拟实现鼠标指针停止运动事件
2016/01/12 Javascript
AngularJS1.X学习笔记2-数据绑定详解
2017/04/01 Javascript
Vue应用部署到服务器的正确方式
2017/07/15 Javascript
js学习总结之DOM2兼容处理重复问题的解决方法
2017/07/27 Javascript
js实现省市级联效果分享
2017/08/10 Javascript
ES6新增的数组知识实例小结
2020/05/23 Javascript
[51:17]VGJ.T vs Mineski 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
Python Matplotlib库入门指南
2015/05/18 Python
不要用强制方法杀掉python线程
2017/02/26 Python
python 获取一个值在某个区间的指定倍数的值方法
2018/11/12 Python
python实现列表中最大最小值输出的示例
2019/07/09 Python
python3安装crypto出错及解决方法
2019/07/30 Python
python3的UnicodeDecodeError解决方法
2019/12/20 Python
PyCharm2020.3.2安装超详细教程
2021/02/08 Python
在线学习西班牙语、法语或其他语言:Babbel.com
2018/02/07 全球购物
德国香水、化妆品和护理产品网上商店:Parfumdreams
2018/09/26 全球购物
为什么会有内存对齐
2016/10/10 面试题
2014年社区工会工作总结
2014/12/18 职场文书
业务内勤岗位职责
2015/04/13 职场文书
Filebeat 采集 Nginx 日志的方法
2021/03/31 Servers
一劳永逸彻底解决pip install慢的办法
2021/05/24 Python