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
/**
* 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();