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#编写的类 ...
随机推荐
- 紫 distance
仅此纪念我爆掉的T3 紫,即RE,运行出错,梦幻,而又不失杀气 根据<雪distance>改编,分为提交前,评测前,评测时,评测后 你说我考试AK,可我却运行出错 任凭无尽的懊悔将我淹没, ...
- paddleNLP-BUG和一些细节记录【一】
1.TypeError: isfile() takes 1 positional argument but 2 were given File "/root/miniconda3/envs/ ...
- 【编写环境一】遇到常见python函数处理方式
1.python实现两个一维列表合并成一个二维列表 >>> list1 = [1,2,3,4,4] >>> list2 = [2,3,4,5,2] >> ...
- 14.5 Socket 应用组播通信
组播通信是一种基于UDP协议的网络通信方式,它允许发送方将消息同时传递给多个接收方.在组播通信中,发送方和接收方都会加入一个共同的组播组,这个组播组对应一个特定的IP地址,所有加入该组播组的主机都能够 ...
- Vue3学习笔记 —— 状态管理、Vuex、Pinia (未完结)
优秀文章分享:vue中使用vuex(超详细) - 掘金 (juejin.cn) 一.状态管理 1.1.什么是状态管理? 理论上来说,每一个 Vue 组件实例都已经在"管理"它自己的 ...
- 对未来的自己的一个提醒。关于打表答题的思路,洛谷P5731
P5731 [深基5.习6]蛇形方阵 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 这道题就是纯纯找规律的模拟题,但是在比赛或者思维比较松散的情况下紧张的时候会想不出模拟思路 这时 ...
- DHCP的安装与配置
一:前期准备 1.打开windows虚拟机,使用仅主机模式 (虚拟机(M)→设置(S)→网络适配器) 2.修改Windows ip可选范围为192.168.1.204到192.168.1.207 对应 ...
- Pandas分析泰坦尼克号生还比例
提出问题 影响乘客生还的因素很多,这里只对乘客的性别.年龄.乘客等级.这三个因素感兴趣, 看看这四个因素是否会影响乘客的生还率. 1.性别是否会影响生还率 2.年龄是否会影响生还率 3.乘客等级会否会 ...
- JS leetcode 存在重复元素 II 题解分析,记一次震惊的负向优化
壹 ❀ 引 整理下今天做的算法题,题目难度不高,但在优化角度也是费了一些功夫.题目来自219. 存在重复元素 II,问题描述如下: 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i ...
- NC223888 红色和紫色.md
题目链接 题目 题目描述 漫长的生命总是无聊的.这天,小红和紫准备玩一个染色游戏. 她们拿出了一个有 \(n*m\) 个格子的网格,每个格子只能被染成红色或紫色.每个人可以任意选择一个格子染成红色和紫 ...