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#编写的类 ...
随机推荐
- springboot整合nacos的入门Demo
Nacos介绍 Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service的首字母简称,一个更易于构建云原生应用的动态服务发现.配置管理和服务 ...
- PE格式:新建节并插入DLL
首先老样子,我们先来到PE节表位置处,并仿写一个.hack的节,该节大小为0x1000字节,在仿写前我们需要先来计算出.hack的虚拟偏移与实际偏移,先来查询一下当前节表结构,如下: 接着我们通过公式 ...
- iOS 常用命令行工具总结
平时工作中会经常用到命令行工具Command Lines Tool.而Command Line Tool本质是一个命令行工具包,内部有很多有用的工具,如Apple LLVM compiler.Make ...
- React核心概念与JSX
React概况 React是一个只用来写HTML的UI页面的JS库,在MVC设计模式中它只相当于View,故:它并不是一个框架(MVC架构角色设计). React组件内数据改动会自动更新到屏幕上. R ...
- workman在线五子棋
一.下载安装workman,地址:https://github.com/walkor/workerman composer require workerman/workerman 二.cd到worke ...
- (C语言)课后题之计算器
#include <stdio.h> void main() { //定义两个算术变量,四个运算结果变量 int a,b,sum,sub,mul,mod; double div; prin ...
- Oracle私网mtu滚动修改实施方案
之前测试遇到过mtu修改不能滚动的情况,目前在自己测试环境重新反复验证发现正常是可以滚动的,下面梳理下整个实施方案: 环境:RHEL6 + Oracle 11.2.0.4 RAC(2 nodes) / ...
- 6.用户输入和 while 循环--《Python编程:从入门到实践》
6.1 input 函数 函数input()接受一个参数:即要向用户显示的提示或说明.input 将用户输入解释为字符串. name = input("Please enter your n ...
- Linux 在线安装MySQL8.0
1.更新Linux yum yum update 2.安装wget工具(如果已经安装wget,可以跳过该步骤) yum install wget 3.使用wget下载MySQL Yum Reposit ...
- 【译】.NET 8 网络改进(一)
原文 | Máňa,Natalia Kondratyeva 翻译 | 郑子铭 随着新的 .NET 版本的发布,发布有关网络空间中新的有趣变化的博客文章已成为一种传统.今年,我们希望引入 HTTP 空间 ...