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#编写的类 ...
随机推荐
- C# 理解委托与事件(烧水壶例子)
引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...
- 为游戏接入ios sdk的oc学习笔记
开发手机游戏,需要接入ios的sdk,截止2021年7月23日虽然swift已经推出一些年头,但对于大部分的渠道sdk,还是oc的代码. oc不仅仅用来开发ios,还是mac上的app开发语言 从新手 ...
- TienChin 运行 RuoYi-Vue3
在前几篇文章当中,之前使用的是 Vue2,在某一天发现若依提供了 Vue3 的版本,所以这篇文章主要是运行起来,Vue2,迟早要被替代,所以这里采用最先进的 Vue3. 仓库地址:https://gi ...
- 声明式API和命令式API的区别
声明式API 声明式和命令式的对比 Kubernetes 声明式 API 的工作原理 参考 声明式API 声明式和命令式的对比 命令式 命令式有时也称为指令式,命令式的场景下,计算机只会机械的完成指定 ...
- SpringCloud-01-Eureka Ribbon
1.微服务技术 2.SpringCloud SpringCloud是目前国内使用最广泛的微服务框架.官网地址:https://spring.io/projects/spring-cloud. Spri ...
- 使用ethtool排查网卡速率问题
今天去现场帮一个客户排查备份网络速率问题. 用户期望是万兆的速率,但实际上目前只有千兆,因为目前上面运行着数据库,且数据量较大,千兆的备份网络速率不能满足用户备份数据库的时长要求. 首先,确认备份网络 ...
- CF1878C Vasilije in Cacak 题解
题目传送门 简化题意 有 \(t\) 组询问,每次询问是否能从 \(1 \sim n\) 中选择 \(k\) 个数使得它们的和为 \(x\). 解法 考虑临界情况,从 \(1 \sim n\) 中选择 ...
- 2024 SICTF Round#3出题 crypto misc osint
有幸参与了本次比赛crypto misc OSINT出题,难易程度循序渐进,下面记录一下本人题目题解(( 比赛网址:https://yuanshen.life/ CRYPTO SuperbRSA(85 ...
- 【Unity3D】Unity3D技术栈
1 前言 本文梳理了笔者在学习 Unity3D 的过程中,对 Unity3D 的理解和学习路线,以帮助读者循序渐进地学习 Unity3D,后续笔者仍会持续更新 Unity3D 相关技术栈,并同步到 ...
- Spring Cloud Openfeign微服务接口调用与Hystrix集成实战
关于openfeign 可以认为OpenFeign是Feign的增强版,不同的是OpenFeign支持Spring MVC注解.OpenFeign和Feign底层都内置了Ribbon负载均衡组件,在导 ...