This article mainly introduces how to use PHP to create a perpetual calendar and several key points for implementing this function and provides all the source code. Here we recommend you to your friends to use PHP to implement the Perpetual Calendar function:
Returns the total number of days of the current month to be processed. $ days
Get the number one of the months to be processed is the day of the week $ dayofweek
$ Days: If you know the total number of days of the month to be processed, You can output the number of days in a loop.
$ Dayofweek: only when the first day of each month is the day of the week can we know how many spaces (blank) need to be output before the number of days of output)
The final result is as follows:
The code for "Perpetual Calendar" is as follows:
The Code is as follows:
<? Php
/**
* 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 of the given month
Protected $ _ dayofweek; // the day of the week on the first day of the 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) {// process the case where the month is greater than 12
$ This-> _ month = 1;
$ This-> _ year ++;
}
If ($ this-> _ month <1) {// process the case where the month is smaller 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 )); // obtain the number of days of the given month
$ This-> _ dayofweek = date ("w", mktime (0, 0, $ this-> _ month, 1, $ this-> _ year )); // obtain the number 1 of the given month as the 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 1$ This-> _ table. ="
| ";}For ($ I = 1; $ I <= $ this-> _ days; $ I ++) {// output the number of daysIf ($ nums % 7 = 0) {// line feed processing: 7 rows$ This-> _ table. ="
$ I |
";} Else {$ This-> _ table. ="
$ I | ";}$ Nums ++;}$ This-> _ table. ="
";
$ This-> _ table. = "_ year)." & m = ". ($ this-> _ month-1)." '> last month ";
$ This-> _ table. = "_ year)." & m = ". ($ this-> _ month + 1)." '> next month ";
}
/**
* Output calendar
*/
Public function showCalendar ()
{
$ This-> _ showTitle ();
$ This-> _ showDate ();
Echo $ this-> _ table;
}
}
$ Calc = new Calendar ();
$ Calc-> showCalendar ();
The effect is not bad. You can make your friends more beautiful.