Home  >  Article  >  Backend Development  >  PHP perpetual calendar implementation code_PHP tutorial

PHP perpetual calendar implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:14:501811browse

Key points of using PHP to implement the perpetual calendar function:

•Get the total number of days in the current month to be processed $days
•Get the day of the week that the first day of the month to be processed is $dayofweek
$days Function: If you know how many days there are in the month to be processed, you can output the number of days through a loop

Function of $dayofweek: Only if you know the day of the week on the 1st of each month, can you know that you need to output the number of days before outputting it. How many spaces (blank)

The final rendering is as follows:

PHP perpetual calendar implementation code_PHP tutorial

The code of "perpetual calendar class" is as follows:

Copy code The code is as follows:

/**
* PHP Perpetual Calendar
* @author Fly 2012/10/16
*/
class Calendar{
protected $_table;/ /table table
protected $_currentDate; //Current date
protected $_year; //Year
protected $_month; //Month
protected $_days; //The given month should have Number of days
protected $_dayofweek;//What day of the week the 1st of a given month is
/**
* Constructor
*/
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){//Handling months greater than 12 Case
$this->_month=1;
$this->_year++;
}
if ($this->_month<1){//Handle the month less than 1 Case
$this->_month=12;
$this->_year--;
}
$this->_currentDate = $this->_year.'year'. $this->_month.'month';//Currently obtained date information
$this->_days = date("t",mktime(0,0,0,$this->_month,1 ,$this->_year));//Get the number of days in a given month
$this->_dayofweek = date("w",mktime(0,0,0,$this-> _month,1,$this->_year));//Get the day of the week that the 1st of a given month is
}
/**
* Output title and header information
*/
protected function _showTitle()
{
$this->_table="";
$this->_table.="";
$this->_table .="";
$this->_table .="";
$this->_table .="";
$this->_table .="";
$ this->_table .="";
$this->_table .="";
$this- >_table .="";
$this->_table.="";
}
/**
* Output date information
* Output date information based on the current date
*/
protected function _showDate()
{
$nums=$this->_dayofweek+1;
for ($i=1;$i< =$this->_dayofweek;$i++){//Output the blank date before the 1st
$this->_table.="";
}
for ($i=1;$i<=$this->_days;$i++){//Output the number of days information
if ($nums%7==0){//Line break processing: 7 per line
$this->_table.="";
}else{
$this->_table.= "";
}
$nums++;
}
$this->_table.="
".$this->_currentDate ."
SundayMondayTuesdayWednesdayThursdayFridaySaturday
 $i
$i
" ;
$this->_table.="

Previous month ";
$this->_table.="Next month

";
}
/**
* Output calendar
*/
public function showCalendar()
{
$this->_showTitle();
$this->_showDate();
echo $this->_table;
}
}
$calc=new Calendar();
$calc->showCalendar();

Source: http://www.cnblogs.com/hongfei

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326201.htmlTechArticleKey points of using PHP to implement the perpetual calendar function: Get the total number of days in the current month to be processed $days Get the current number of days to be processed The first day of the month is the day of the week $dayofweek The role of $days: know...
Statement:
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