Home php教程 php手册 PHP万年历实现程序代码

PHP万年历实现程序代码

May 25, 2016 pm 04:43 PM
php code php program

使用PHP实现万年历功能的要点:

得到当前要处理的月份总共有多少天$days,得到当前要处理的月份的一号是星期几$dayofweek,$days的作用:知道要处理的月份共有多少天,就可以通过循环输出天数了,$dayofweek的作用:只有知道每个月的1号是星期几,才能知道在输出天数之前需要输出多少空格(空白).

PHP完整实例代码如下:

<?php 
	/** 
	 * PHP万年历 
	 * @author Fly 2012/10/16 
	 */ 
	class Calendar{ 
	    protected $_table;//table表格 
	    protected $_currentDate;//当前日期 
	    protected $_year;    //年 
	    protected $_month;    //月 
	    protected $_days;    //给定的月份应有的天数 
	    protected $_dayofweek;//给定月份的 1号 是星期几 
	    /** 
	     * 构造函数 
	     */ 
	    public function __construct()  
	    { 
	        $this->_table=""; 
	        $this->_year  = isset($_GET["y"])?$_GET["y"]:date("Y"); 
	        $this->_month = isset($_GET["m"])?$_GET["m"]:date("m"); 
	        if ($this->_month>12){//处理出现月份大于12的情况 
	            $this->_month=1; 
	            $this->_year++; 
	        } 
	        if ($this->_month<1){//处理出现月份小于1的情况 
	            $this->_month=12; 
	            $this->_year--; 
	        } 
	        $this->_currentDate = $this->_year.&#39;年&#39;.$this->_month.&#39;月份&#39;;//当前得到的日期信息 
	        $this->_days           = date("t",mktime(0,0,0,$this->_month,1,$this->_year));//得到给定的月份应有的天数 
	        $this->_dayofweek    = date("w",mktime(0,0,0,$this->_month,1,$this->_year));//得到给定的月份的 1号 是星期几 
	    } 
	    /** 
	     * 输出标题和表头信息 
	     */ 
	    protected function _showTitle() 
	    { 
	        $this->_table="<table><thead><tr align=&#39;center&#39;><th colspan=&#39;7&#39;>".$this->_currentDate."</th></tr></thead>"; 
	        $this->_table.="<tbody><tr>"; 
	        $this->_table .="<td style=&#39;color:red&#39;>星期日</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=&#39;color:red&#39;>星期六</td>"; 
	        $this->_table.="</tr>"; 
	    } 
	    /** 
	     * 输出日期信息 
	     * 根据当前日期输出日期信息 
	     */ 
	    protected function _showDate() 
	    { 
	        $nums=$this->_dayofweek+1; 
	        for ($i=1;$i<=$this->_dayofweek;$i++){//输出1号之前的空白日期 
	            $this->_table.="<td>&nbsp</td>"; 
	        } 
	        for ($i=1;$i<=$this->_days;$i++){//输出天数信息 
	            if ($nums%7==0){//换行处理:7个一行 
	                $this->_table.="<td>$i</td></tr><tr>";     
	            }else{ 
	                $this->_table.="<td>$i</td>"; 
	            } 
	            $nums++; 
	        } 
	        $this->_table.="</tbody></table>"; 
	        $this->_table.="<h3><a href=&#39;?y=".($this->_year)."&m=".($this->_month-1)."&#39;>上一月</a>   "; 
	        $this->_table.="<a href=&#39;?y=".($this->_year)."&m=".($this->_month+1)."&#39;>下一月</a></h3>"; 
	    } 
	    /** 
	     * 输出日历 
	     */ 
	    public function showCalendar() 
	    { 
	        $this->_showTitle(); 
	        $this->_showDate(); 
	        echo $this->_table; 
	    } 
	} 
	$calc=new Calendar(); 
	$calc->showCalendar(); 
Copy after login

教程地址:

欢迎转载!但请带上文章地址^^

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Best practices for routing management in PHP programs Best practices for routing management in PHP programs Aug 25, 2023 pm 12:28 PM

Route management is one of the most critical parts of any web application because they determine how a URL request will be processed and responded to. PHP is a widely used web programming language and many developers use PHP to build their web applications. In this article, we will discuss the best practices for routing management in PHP programs. Using the MVC Framework Many PHP applications are developed using the MVC (Model-View-Controller) framework. In this framework,

How to write PHP code in the browser and keep the code from being executed? How to write PHP code in the browser and keep the code from being executed? Mar 10, 2024 pm 02:27 PM

How to write PHP code in the browser and keep the code from being executed? With the popularization of the Internet, more and more people have begun to come into contact with web development, and learning PHP has also attracted more and more attention. PHP is a scripting language that runs on the server side and is often used to write dynamic web pages. However, during the exercise phase, we want to be able to write PHP code in the browser and see the results, but we don't want the code to be executed. So, how to write PHP code in the browser and keep it from being executed? This will be described in detail below. first,

How to use GitHub Actions for automated packaging and deployment of PHP programs? How to use GitHub Actions for automated packaging and deployment of PHP programs? Jul 31, 2023 pm 02:28 PM

How to use GitHubActions for automated packaging and deployment of PHP programs? Introduction With the rise of cloud computing and DevOps, automation and continuous integration of software development have become increasingly important. GitHubActions is a powerful automation tool that can help developers achieve rapid and efficient software development and deployment. In this article, we will focus on how to use GitHubActions for automated packaging and deployment of PHP programs to improve development efficiency. 1. Assume

Best practices for performance optimization in PHP programs Best practices for performance optimization in PHP programs Jun 06, 2023 am 09:20 AM

PHP is a popular programming language that is widely used for website and web application development. However, when PHP applications become more and more complex, performance issues also manifest themselves. Therefore, performance optimization has become an important aspect in PHP development. In this article, we will introduce optimization best practices in PHP programs to help you improve the performance of your applications. 1. Choose the correct PHP version and extensions First, make sure you are using the latest PHP version. New releases usually include performance improvements and bug fixes, as well as

How to use regular expressions to batch modify PHP code to meet the latest code specifications? How to use regular expressions to batch modify PHP code to meet the latest code specifications? Sep 05, 2023 pm 03:57 PM

How to use regular expressions to batch modify PHP code to meet the latest code specifications? Introduction: As time goes by and technology develops, code specifications are constantly updated and improved. During the development process, we often need to modify old code to comply with the latest code specifications. However, manual modification can be a tedious and time-consuming task. In this case, regular expressions can be a powerful tool. Using regular expressions, we can modify the code in batches and automatically meet the latest code specifications. 1. Preparation: before using

PHP code implements request parameter encryption and decryption processing of Baidu Wenxinyiyan API interface PHP code implements request parameter encryption and decryption processing of Baidu Wenxinyiyan API interface Aug 16, 2023 pm 11:40 PM

The PHP code implements the request parameter encryption and decryption processing of Baidu Wenxin Yiyan API interface. Hitokoto is a service that provides access to random sentences. Baidu Wenxin Yiyan API is one of the interfaces that developers can call. In order to ensure data security, we can encrypt the request parameters and decrypt the response after receiving the response. The following is an example of PHP code implementing the request parameter encryption and decryption processing of Baidu Wenxinyiyan API interface: &lt;?phpfunction

How to use PHP code testing function to improve code maintainability How to use PHP code testing function to improve code maintainability Aug 11, 2023 pm 12:43 PM

How to use the PHP code testing function to improve the maintainability of the code. In the software development process, the maintainability of the code is a very important aspect. A maintainable code means that it is easy to understand, easy to modify, and easy to maintain. Testing is a very effective means of improving code maintainability. This article will introduce how to use PHP code testing function to achieve this purpose, and provide relevant code examples. Unit testing Unit testing is a testing method commonly used in software development to verify the smallest testable unit in the code. in P

Analyze PHP code testing function and its importance Analyze PHP code testing function and its importance Aug 11, 2023 pm 03:12 PM

Analyzing the PHP code testing function and its importance Preface: In the software development process, code testing is an indispensable link. By testing the code, potential bugs and errors can be effectively discovered and resolved, and the quality and stability of the code can be improved. In PHP development, testing functions is also important. This article will delve into the function and importance of PHP code testing, and illustrate it with examples. 1. Functional unit testing (UnitTesting) of PHP code testing Unit testing is the most common testing method

See all articles