PHP production perpetual calendar, php perpetual calendar
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
The role of $days: knowing how many days there are in the month to be processed, you can output the number of days through a loop
The role of $dayofweek: Only by knowing the day of the week on the 1st of each month can we know how many spaces (blanks) need to be output before outputting the number of days
The final rendering is as follows:
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
Protected $_currentDate;//Current date
Protected $_year; //Year
Protected $_month; //Month
Protected $_days; //Number of days in a given month
protected $_dayofweek;//What day of the week is the 1st of a given month
/**
* 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){//Handle the situation where the month is greater than 12
$this->_month=1;
$this->_year++;
}
If ($this->_month<1){//Handle the situation where the month is less than 1
$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 1st of the given month Day of the week
}
/**
* Output title and header information
*/
Protected function _showTitle()
{
$this->_table="
".$this->_currentDate." |
";
$this->_table.="";
$this->_table .="Sunday | ";
$this->_table .="Monday | ";
$this->_table .="Tuesday | ";
$this->_table .="Wednesday | ";
$this->_table .="Thursday | ";
$this->_table .="Friday | ";
$this->_table .="Saturday | ";
$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 days information
if ($nums%7==0){//Line break processing: 7 per line
$this->_table.="$i | ";
}else{
$this->_table.="$i | ";
}
$nums++;
}
$this->_table.="
";
$this->_table.="
上一月 ";
$this->_table.="下一月
";
}
/**
* * Output calendar
*/
public function showCalendar()
{
$this->_showTitle();
$this->_showDate();
echo $this->_table;
}
}
$calc=new Calendar();
$calc->showCalendar();
效果还不错吧,小伙伴们还可以自己美化下,这里就不多做说明了。
http://www.bkjia.com/PHPjc/939405.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/939405.htmlTechArticlePHP制作万年历,php万年历 使用PHP实现万年历功能的要点: 得到当前要处理的月份总共有多少天$days 得到当前要处理的月份的一号是星期几...