php编写日历类
<?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编写日历类的更多相关文章
- 22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。
22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表.然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法sh ...
- 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 ...
- 实现如下类之间的继承关系,并编写Music类来测试这些类。
实现如下类之间的继承关系,并编写Music类来测试这些类. package com.hanqi.test; public class Instrument { //输出弹奏乐器 public void ...
- NSCalenda日历类
1. //将数据库时间和当前时间相比,得出时间差. + (NSString *)dateDescriptionWithDate:(NSDate *)date{ // NSCalendar日历类,提供了 ...
- 编写测试类,了解ArrayList的方法
这篇文章主要介绍了C#中动态数组用法,实例分析了C#中ArrayList实现动态数组的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#中动态数组用法.分享给大家供大家参考.具体分析如下 ...
- C++编写Config类读取配置文件
老外写的一段代码,在Server中编写这个类读取配置文件比较实用 //Config.h #pragma once #include <string> #include <map> ...
- iOS 日历类(NSCalendar)
对于时间的操作在开发中很常见,但有时候我们需要获取到一年后的时间,或者一周后的时间.靠通过秒数计算是不行的.那就牵扯到另外一个日历类(NSCalendar).下面先简单看一下 NSDate let d ...
- ADO.NET复习——自己编写SqlHelper类
今天复习了一次ADO.NET基础,整理一下自己的认为的重点: 编写SqlHelper类,方便我们执行数据库语句,这时可以直接调用封装在SqlHelper类的方法.现在大多数公司面试的时候,给你的面试题 ...
- Swift互用性: 使用Objective-C特性编写Swift类(Swift 2.0版)-b
本节包括内容: 继承Objective-C的类(Inheriting from Objective-C Classes) 采用协议(Adopting Protocols) 编写构造器和析构器(Writ ...
- 在C++中使用C#编写的类2
在那篇<在C#中使用C++编写的类>中我介绍了如何在C#中使用C++编写的类.可是由于C#在用户界面设计.数据库存储和XML文件读取等方面的优势,有时候也会出现要在C++中使用C#编写的类 ...
随机推荐
- Python 探索性数据分析工具(PandasGUI,Pandas Profiling,Sweetviz,dtale)以及学术论文快速作图science.mplstyle
如果探索的数据集侧重数据展示,可以选PandasGUI:如果只是简单了解基本统计指标,可以选择Pandas Profiling和Sweetviz:如果需要做深度的数据探索,那就选择dtale. 1. ...
- parser.add_argument()用法——命令行选项、参数和子命令解析器
argparse是一个Python模块:命令行选项.参数和子命令解析器.通过使用这种方法,可以在使用 1.argparse简介: argparse 模块是 Python 内置的一个用于命令项选项与参数 ...
- Linux文件IO之二 [补档-2023-07-21]
8-5 linux系统IO函数: open函数: 函数原型:int open(const char *pathname, int flags, mode_t mode); 功能:打开一个文件并 ...
- Bootstrap Table 动态修改行的颜色
Bootstrap Table 官网地址 https://bootstrap-table.com/百度搜了大量资料 还是找不 动态改变行的颜色,一般搜索到的都是 初始化的时候 使用 rowStyle ...
- IDEA破解(无限重启激活时间版)
下载地址[将下载的目录打成zip压缩包后使用]:「ide-eval-resetter」https://www.aliyundrive.com/s/UFHpDX5d6Xv 点击链接保存,或者复制本段内容 ...
- Java并发编程面试题
Synchronized 用过吗,其原理是什么? Synchronized是jvm实现的一种互斥同步访问方式,底层是基于对象的监视器monitor实现的. 被synchronize修饰的代码在反编译后 ...
- 微服务保护-Sentinel
1.初识Sentinel 1.1.雪崩问题及解决方案 1.1.1.雪崩问题 微服务中,服务间调用关系错综复杂,一个微服务往往依赖于多个其它微服务. 如图,如果服务提供者I发生了故障,当前的应用的部分业 ...
- 《熬夜整理》保姆级系列教程-玩转Wireshark抓包神器教程(1)-初识Wireshark
1.简介 前边已经介绍过两款抓包工具,应该是够用了,也能够处理在日常工作中遇到的问题了,但是还是有人留言让宏哥要讲解讲解Wireshark这一款抓包工具,说实话宏哥之前也没有用过这款工具,只能边研究边 ...
- ASP.NET Core分布式项目实战(oauth2 + oidc 实现 server部分)--学习笔记
任务15:oauth2 + oidc 实现 server部分 基于之前快速入门的项目(MvcCookieAuthSample): https://www.cnblogs.com/MingsonZhen ...
- Spring Boot 1.5.x 结合 JUnit5 进行接口测试
在Spring Boot 1.5.x中,默认使用Junit4进行测试.而在对Controller进行接口测试的时候,使用 @AutoConfigureMockMvc 注解是不能注入 MockMvc 对 ...