<?php

/**
* 日历类
* Class Calendar
* @author fengzi
* @date 2022-05-05 15:42
*/
class Calendar{
protected $table = '';
protected $year;
protected $month;
protected $day;
protected $days;
protected $currentDate;
protected $space;
protected $daysUp;
protected $daysDown; function __construct() {
date_default_timezone_set('PRC');
$this->year = isset($_GET['y']) ? $_GET['y'] : date('Y', time()); //变动的年份 $this->month = isset($_GET['m']) ? $_GET['m'] : date('n', time()); //变动的月份
if ( intval($this->month) > 12) {
$this->month = 1;
$this->year++;
}
if ( intval($this->month) < 1) {
$this->month = 12;
$this->year--;
} $this->day = date('j', time()); //当月第几天
$this->months = date('n', time()); //当前月份
$this->currentDate = $this->year." 年 ".$this->month." 月";
$this->space = date('w', mktime(0, 0, 0, $this->month, 1, $this->year)); //每月的 1号 是星期几
$this->days = date("t",mktime(0,0,0,$this->month,1,$this->year));//得到给定的月份应有的天数
$this->daysUp = date("t",mktime(0,0,0,$this->month-1,1,$this->year));//得到给定的月份上一个月的应有的天数
$this->daysDown = date("t",mktime(0,0,0,$this->month+1,1,$this->year));//得到给定的月份下一个月的应有的天数
} //拼接表
protected function mosaicTable() {
$this->table .= "<table><thead><tr align='center'><th colspan='7'>".$this->currentDate."</th></tr></thead>";
$this->table .= "<tbody><tr>";
$this->table .= "<td style='color:red;'>星期日</td>";
$this->table .= "<td>星期一</td>";
$this->table .= "<td>星期二</td>";
$this->table .= "<td>星期三</td>";
$this->table .= "<td>星期四</td>";
$this->table .= "<td>星期五</td>";
$this->table .= "<td style='color:red;'>星期六</td>";
$this->table .= "</tr>";
} //填日期
protected function fillingDate() {
$this->table .= "<tr>"; //补足第一行前面淡色部分
$up = $this->daysUp - $this->space + 1;
if ( (int)$this->space !== 0 ) {
for ($i=0; $i < $this->space; $i++) {
$this->table .= "<td style='color:#aaa;'>$up</td>";
$up++;
}
} $nums = $this->space;
for ($i=1; $i <= $this->days; $i++) {
if ( $nums % 7 != 0 ) {
if ($i == (int)$this->day && $this->month == $this->months) {
$this->table .= "<td style='color:red;'>$i</td>";
} else {
$this->table .= "<td>$i</td>";
}
} else {
if ($i == (int)$this->day && $this->month == $this->months) {
$this->table .= "</tr><tr><td style='color:red;'>$i</td>";
} else {
$this->table .= "</tr><tr><td>$i</td>";
}
} $nums++;
} //补足最后一行后面淡色部分
$down = 6 - ($nums % 7) + 1;
if ( ($nums % 7) != 0 ) {
for ($i=1; $i <= $this->daysDown; $i++) {
if ( $i > $down) {
break;
}
$this->table .= "<td style='color:#aaa;'>$i</td>";
}
} $this->table .= "</tr></tbody></table>";
} //增减月份
protected function increaseAndDecreaseMonth() {
$this->table .= "<h3><a href='?y=".($this->year)."&m=".($this->month-1)."'>上一月</a> ";
$this->table .= "<a href='?y=".($this->year)."&m=".($this->month+1)."'>下一月</a></h3>";
} //显示日历表
public function showTable() {
$this->mosaicTable();
$this->fillingDate();
$this->increaseAndDecreaseMonth();
echo $this->table;
}
} $Calendar = new Calendar();
$Calendar->showTable();

php编写日历类的更多相关文章

  1. 22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。

    22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表.然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法sh ...

  2. 35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n); (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和; (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n

      35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n): (2)编写一个类:ClassA来实现接口InterfaceA,实现in ...

  3. 实现如下类之间的继承关系,并编写Music类来测试这些类。

    实现如下类之间的继承关系,并编写Music类来测试这些类. package com.hanqi.test; public class Instrument { //输出弹奏乐器 public void ...

  4. NSCalenda日历类

    1. //将数据库时间和当前时间相比,得出时间差. + (NSString *)dateDescriptionWithDate:(NSDate *)date{ // NSCalendar日历类,提供了 ...

  5. 编写测试类,了解ArrayList的方法

    这篇文章主要介绍了C#中动态数组用法,实例分析了C#中ArrayList实现动态数组的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#中动态数组用法.分享给大家供大家参考.具体分析如下 ...

  6. C++编写Config类读取配置文件

    老外写的一段代码,在Server中编写这个类读取配置文件比较实用 //Config.h #pragma once #include <string> #include <map> ...

  7. iOS 日历类(NSCalendar)

    对于时间的操作在开发中很常见,但有时候我们需要获取到一年后的时间,或者一周后的时间.靠通过秒数计算是不行的.那就牵扯到另外一个日历类(NSCalendar).下面先简单看一下 NSDate let d ...

  8. ADO.NET复习——自己编写SqlHelper类

    今天复习了一次ADO.NET基础,整理一下自己的认为的重点: 编写SqlHelper类,方便我们执行数据库语句,这时可以直接调用封装在SqlHelper类的方法.现在大多数公司面试的时候,给你的面试题 ...

  9. Swift互用性: 使用Objective-C特性编写Swift类(Swift 2.0版)-b

    本节包括内容: 继承Objective-C的类(Inheriting from Objective-C Classes) 采用协议(Adopting Protocols) 编写构造器和析构器(Writ ...

  10. 在C++中使用C#编写的类2

    在那篇<在C#中使用C++编写的类>中我介绍了如何在C#中使用C++编写的类.可是由于C#在用户界面设计.数据库存储和XML文件读取等方面的优势,有时候也会出现要在C++中使用C#编写的类 ...

随机推荐

  1. 佳能F-789SGA升级与超频——互联网Tips存档

    佳能F-789SGA简介 佳能F-789SGA是一款性价比极高的科学函数计算器,功能与卡西欧fx-991ES近似,稍强于991ES,弱于991CNX. 来自電卓院亜紀良的评价 来自杨高峰的对比总结 来 ...

  2. 7.1 Windows驱动开发:内核监控进程与线程回调

    在前面的文章中LyShark一直在重复的实现对系统底层模块的枚举,今天我们将展开一个新的话题,内核监控,我们以监控进程线程创建为例,在Win10系统中监控进程与线程可以使用微软提供给我们的两个新函数来 ...

  3. 8、数据库学习规划:MS SQL Server - 学习规划系列文章

    微软的SQL Server数据库是笔者最先接触的数据库,虽然之前有Access,但是那个是学校里知道的,没实际去开发基于Access的程序.SQL Server发展到现在已经有很多个版本了,其功能也非 ...

  4. Leetcode刷题第六天-回溯

    131:分割回文串 链接:131. 分割回文串 - 力扣(LeetCode) for 遍历字符串 递归切割,切割到字符串尾,单次结束 1 class Solution: 2 def partition ...

  5. .NET中使用BootstrapBlazor组件库Table实操篇

    前言 Table表格在后台管理应用中使用的是相当频繁的,因此找一个功能齐全的前端框架对于我们而言是非常必要的,因为封装完善的前端框架能够大大提升我们的工作对接效率.今天我们主要来讲解一下在.NET中使 ...

  6. DHCP中继代理配置与管理

    实验介绍:DHCP中继存在目的 当一台DHCP需要配置不同网段的IP地址时 一:前期准备 1.在DHCP服务器配置页面 右键ipv4,建立多个作用域. 我这里设置了三个可以分配给服务器端的网段,分别是 ...

  7. P8670 [蓝桥杯 2018 国 B] 矩阵求和 题解

    题目传送门 前置知识 欧拉函数 解法 欧拉反演,简单地推下式子即可. \(\begin{aligned}\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} \gcd ...

  8. Buffer Queue原理

    BufferQueue详解 原理一.BufferQueue 简介在工作中,我们经常会和Surface,ImageReader,ImageWriter BufferQueue打交道,它们之间是什么关系呢 ...

  9. VUE2的前端学习笔记

    名词笔记 Node node.js 是一切的基础, 其他的名词都是基于这个运行的, 相当于一个JavaScript的容器或者虚机 NPM 安装node.js时赠送的包管理器, 类似于apt, yum, ...

  10. wordpress设置固定链接404及伪静态配置

    说明 最近在将wordpress设置中文章url修改为月份和名称型 之后访问文章出现404.原因是配有配置好apache的伪静态. 配置步骤 1.修改httpd.conf 我这里是centos7,默认 ...