PHP perpetual calendar and php perpetual calendar. PHP perpetual calendar: get the total number of days of the current month to be processed $ days get the first day of the current month to be processed is the day of the week PHP perpetual calendar, php perpetual calendar
Key points for implementing the perpetual calendar function using PHP:
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.
Tips: use PHP to implement perpetual calendar: get the total number of days of the current month to be processed $ days get the number of days of the current month to be processed is the number of days of the week...