Home > Backend Development > PHP Tutorial > Detailed explanation of how to make a simple calendar with PHP (with code)

Detailed explanation of how to make a simple calendar with PHP (with code)

藏色散人
Release: 2023-04-11 13:02:01
forward
3647 people have browsed it

This article brings you relevant knowledge about PHP. It mainly introduces how to use PHP to make a simple calendar. Friends who are interested can take a look below. I hope it will be helpful to everyone.

##Example description

Speaking of right For date and time processing, we must introduce the writing of calendar programs. But when it comes to writing a calendar, most readers will think that the role of the calendar is only to display the current date on the page. In fact, the calendar plays a more important role in our development. For example, when we develop a "notepad", we need to set the date through the calendar. In some systems, we need to arrange tasks by date and also need a calendar, etc.

Implementation process

Declare the calendar class Calendar in the file calendar.class.php, the code is as follows:

<?php
/*
	calendar.class.php日历类
	声明一个日历类,名称为Calendar,用来显示可以设置日期的日历
 */
class Calendar{
	private $year;//当前的年
	private $month;//当前的月
	private $start_weekday;//当月的第一天对应的是周几,作为当月遍历日期的开始
	private $days;//当前月的总天数

	/**
	 * 构造方法,初始化一些属性
	 */
	function __construct(){ 
		//如果用户没有设置年份数,则使用当前系统时间的年份
		$this->year = isset($_GET["year"]) ? $_GET["year"] :date("Y") ;
		//如果用户没有设置月份数,则使用当前系统时间的月份
		$this->month = isset($_GET["month"]) ? $_GET["month"] :date("m") ;
		//通过具体的年份和月份,利用date() 函数的w参数获取当月第一天对应的是周几
		$this->start_weekday = date("w",mktime(0, 0, 0, $this->month, 1, $this->year));
		//通过具体的年份和月份,利用date()函数的参数获取当月的天数
		$this->days = date("t",mktime(0, 0, 0, $this->month, 1, $this->year));
	}
	/**
	 * 打印整个日历
	 * @return string 日历字符串
	 */
	function __toString(){
		$out .= &#39;<table align="center">&#39;; //日历以表格形式打印
		$out .= $this->changeDate(); //用户设置日期
		$out .= $this->weeksList(); //打印·周·列表
		$out .= $this->daysList(); //打印·日·列表
		$out .= &#39;</table>&#39;; //表格结束
		return $out; //返回整个日历,输出需要的全部字符串 
	}
	/**
	 * 输出周列表
	 * @return string 周列表字符串
	 */
	private function weeksList(){
		$week = array (&#39;日&#39;,&#39;一&#39;,&#39;二&#39;,&#39;三&#39;,&#39;四&#39;,&#39;五&#39;,&#39;六&#39;);
		$out .= &#39;<tr>&#39;;
		for($i = 0; $i < count($week); $i++){
			$out .= &#39;<th class="fontb">&#39; . $week [$i]. &#39;</th>&#39;;
		}
		$out .= &#39;</tr>&#39;;
		return $out; // 返回周列表字符串
	}
	/**
	 * 输出日列表
	 * @return string 日历表字符串
	 */
	private function daysList(){
		$out .= &#39;<tr>&#39;;
		// 输出空格(当月第一天对应的是周几,就是几个空格)
		for($j = 0; $j < $this->start_weekday; $j++){
			$out .= &#39;<td> </td>&#39;;
		}
		// 循环输出当前月所有日期
		for($k = 1; $k <= $this->days; $k++){
			$j++;
			if($k == date(&#39;d&#39;)){// 若为当前日期,设置为深色背景
				$out .= &#39;<td class="fontb">&#39;.$k.&#39;</td>&#39;;
			} else {
				$out .= &#39;<td>&#39;.$k.&#39;</td>&#39;;
			}
			if($j%7 == 0){//每输出7个日期,就换一行
				$out .= &#39;</tr><tr>&#39;;//输出行结束和下一行开始
			}
		}
		while ($j%7 != 0) {//遍历完日期后,剩下的用空格补全
			$out .= &#39;<td> </td>&#39;;
			$j++;
		}
		$out .= &#39;</tr>&#39;;
		return $out; //返回当月日列表
	}
	/**
	 * 用于处理当前年份的上一年需要的数据
	 * @param  int $year  当前年份
	 * @param  int $month 当前月份
	 * @return string   最终的年份和月份设置参数
	 */
	private function prevYear($year, $month){ 
		$year = $year-1; //上一年是当前年减1
		if ($year < 1970){ //如果设至的年份小于1970年
			$year = 1970; //年份设置最小值是1970年
		}
		//返回最终的年份和月份设置参数
		return "year={$year}&month={$month}";
	}
	/**
	 * 用于处理当前月份的上一月份的数据
	 * @param  int $year  当前年份
	 * @param  int $month 当前月份
	 * @return string   最终的年份和月份设置参数
	 */
	private function prevMonth($year, $month){
		if($month== 1){
			$year = $year -1;
			if($year < 1970){ // 最小年份数不能小于1970年
				$year = 1970;
			}
			//如果月是1月,上一月就是上一年的最后一月
			$month = 12;
		} else {
			$month--; //上一月就是当前月减1
		}
		// 返回最终的年份和月份设置参数
		return "year={$year}&month={$month}";
	}
	/**
	 * 用于处理当前年份的下一年份的数据
	 * @param  int $year  当前年份
	 * @param  int $month 当前月份
	 * @return string   最终的年份和月份设置参数
	 */
	private function nextYear($year, $month){
		$year = $year+1; // 下一年是当前年加1
		if ($year> 2038){ //如果设量的年份大于2038年
			$year=2038; //最大年份不能超过2038年
		}
		//返回最终的年份和月份设置参数
		return "year={$year}&month={$month}";
	}
	/**
	 * 用于处理当前月份的下一个月份的数据
	 * @param  int $year  当前年份
	 * @param  int $month 当前月份
	 * @return string   最终的年份和月份设置参数
	 */
	private function nextMonth($year, $month){
		if($month == 12){//如果已经是当年的最后一个月
			$year++;//下一个月就是下一年的第一个月,让年份加1
			if($year> 2038){ //如果设豆的年份大于2038年
				$year = 2038; //最大年份不能超过2038年
			}
			$month = 1; //设置月份为下一年的第一个月
		} else {
			$month++;//其他月份的下一个月都是当前月份加1即可
		}
		//返回最终的年份和月份设置参数
		return "year={$year}&month={$month}";
	}
	/**
	 * 调整日期
	 * @param  string $url 页面路径
	 * @return string 页面字符串
	 */
	private function changeDate($url=&#39;index.php&#39;){
		$out .= &#39;<tr>&#39;;
		//上一年
		$out .= &#39;<td><a href="&#39;.$url.&#39;?&#39;.$this->prevYear($this->year,$this->month).&#39;">&#39;.&#39;<<&#39;.&#39;</a></td>&#39;;
		//上个月
		$out .= &#39;<td><a href="&#39;.$url.&#39;?&#39;.$this->prevMonth($this->year,$this->month).&#39;">&#39;.&#39;<&#39;.&#39;</a> </td>&#39;;
		$out .= &#39;<td colspan="3">&#39;;
		$out .= &#39;<form>&#39;;
		//年份选择框
		$out .= &#39;<select name="year" οnchange="window.location=\&#39;&#39;. $url.&#39;?year=\&#39;+this.options[selectedIndex].value+\&#39;&month=&#39;. $this->month. &#39;\&#39;">&#39;;
		//循环输出年份
		for($sy=1970; $sy <= 2038; $sy++){
			$selected = ($sy==$this->year) ? "selected" : "";
			$out .= &#39;<option &#39;. $selected. &#39; value="&#39;. $sy. &#39;">&#39;. $sy. &#39;</option>&#39;;
		}
		$out .= &#39;</select>&#39;;
		//月份选择框
		$out .= &#39;<select name="month" οnchange="window.location=\&#39;&#39;. $url. &#39;?year=&#39;. $this->year. &#39;&month=\&#39;+this.options[selectedIndex].value">&#39;;
		//循环输出月份
		for ($sm=1; $sm <=12; $sm++){
			$selected1 = ($sm==$this->month) ? "selected" : "";
			$out .=&#39;<option &#39;. $selected1. &#39; value="&#39;. Ssm. &#39;">&#39;. $sm. &#39;</option>&#39;;
		}
		$out .= &#39;</select>&#39;;
		$out .= &#39;</form>&#39;;
		$out .= &#39;</td>&#39;;
		//下一年
		$out .= &#39;<td> <a href="&#39;.$url.&#39;?&#39;.$this->nextMonth($this->year,$this->month).&#39;">&#39;.&#39;>&#39;.&#39;</a></td>&#39;;
		//下个月
		$out .= &#39;<td> <a href="&#39;.$url.&#39;?&#39;.$this->nextYear($this->year,$this->month).&#39;">&#39;.&#39;>>&#39;.&#39;</a></td>&#39;;
		$out .= &#39;</tr>&#39;;
		return $out; //返回调整日期的表单
	}
}
Copy after login

This example splits a calendar program by function (week list part, date list part, set date part, and setting part for the previous year, next year, previous month and next month) and encapsulates it in a calendar class. With the calendar class, we also need to write a main program to load and output the calendar. In the main program, you also need to set the calendar output style first. The code is as follows:


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>日历示例</title>
<style>
table{ border: 1px solid #050; }
.fontb{ color:white;background:blue; }
th { width: 30px; }
td,th { height: 30px;text-align:center; }
form { margin: 0px;padding: 0px; }
</style>
</head>
<body>
<?php
require &#39;calendar.class.php&#39;;
echo new Calendar;
?>
</body>
</html>
Copy after login

Effect display

Run The effect is as follows:


Detailed explanation of how to make a simple calendar with PHP (with code)

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of Detailed explanation of how to make a simple calendar with PHP (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template