The essentials of using PHP to realize the calendar function:
• Get the total number of days of the month currently to be processed $days
• Get the number of the month that is currently being processed is the week $dayofweek
$days role: Know how many days to process the month, you can output the number of days by cycle
$dayofweek role: Only know the number of days per month is the day of the week, to know how many spaces to output before the number (blank)
The final effect figure is as follows:
The code for the "Perpetual calendar class" is as follows:
Copy Code code as follows:
<?php
/**
* PHP million calendar
* @author Fly 2012/10/16
*/
Class calendar{
Protected $_table;//table Form
Protected $_currentdate;//Current Date
protected $_year; Years
protected $_month; Month
protected $_days; The number of days due for a given month
Protected $_dayofweek;//number 1th for a given month is the day of the week.
/**
* Constructor
*/
Public Function __construct ()
{
$this->_table= "";
$this->_year = isset ($_get["y"])? $_get["Y"]:d ate ("Y");
$this->_month = isset ($_get["M"])? $_get["M"]:d ate ("M");
if ($this->_month>12) {//processing occurs when the month is greater than 12
$this->_month=1;
$this->_year++;
}
if ($this->_month<1) {//processing occurs when the month is less than 1
$this->_month=12;
$this->_year--;
}
$this->_currentdate = $this->_year. ' Year '. $this->_month. ' Month '; current 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)); number 1th for the given month is the day of the week.
}
/**
* Output Header and header information
*/
protected function _showtitle ()
{
$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 ' > Sunday </td>";
$this->_table. = "<td> Monday </td>";
$this->_table. = "<td> Tuesday </td>";
$this->_table. = "<td> Wednesday </td>";
$this->_table. = "<td> Thursday </td>";
$this->_table. = "<td> Friday </td>";
$this->_table. = "<td style= ' color:red ' > Saturday </td>";
$this->_table.= "</tr>";
}
/**
* Output date information
* Output date information based on current date
*/
protected function _showdate ()
{
$nums = $this->_dayofweek+1;
For ($i =1 $i <= $this->_dayofweek; $i + +) {//blank date before 1th #
$this->_table.= "<td> </td>";
}
For ($i =1 $i <= $this->_days; $i + +) {//Output days information
if ($nums%7==0) {//linefeed processing: 7 rows
$this->_table.= "<td> $i </td></tr><tr>";
}else{
$this->_table.= "<td> $i </td>";
}
$nums + +;
}
$this->_table.= "</tbody></table>";
$this->_table.= "$this->_table.= "<a href= '? y=". ($this->_year). " &m= ". ($this->_month+1). "' > next month </a>}
/**
* Output Calendar
*/
Public Function Showcalendar ()
{
$this->_showtitle ();
$this->_showdate ();
Echo $this->_table;
}
}
$calc =new Calendar ();
$calc->showcalendar ();
Source: Http://www.cnblogs.com/hongfei