Calendar. class. php
Copy codeThe Code is as follows:
<? Php
Class Calendar {
Private $ year; // current year
Private $ month; // the current month
Private $ start_weekday; // The day of the month corresponds to the day of the week
Private $ days; // The total number of days in the current month
Function _ construct (){
$ This-> year = isset ($ _ GET ["year"])? $ _ GET ["year"]: date ("Y ");
$ This-> month = isset ($ _ GET ["month"])? $ _ GET ["month"]: date ("m ");
$ This-> start_weekday = date ("w", mktime (0, 0, 0, $ this-> month, 1, $ this-> year ));
$ This-> days = date ("t", mktime (0, 0, 0, $ this-> month, 1, $ this-> year ));
}
Function out (){
Echo '<table align = "center"> ';
$ This-> chageDate ("test. php ");
$ This-> weeksList ();
$ This-> daysList ();
Echo '</table> ';
}
Private function weeksList (){
$ Week = array ('day', 'yi', '2', '3', '4', '5', '6 ');
Echo '<tr> ';
For ($ I = 0; $ I <count ($ week); $ I ++)
Echo '<th class = "fontb">'. $ week [$ I]. '</th> ';
Echo '</tr> ';
}
Private function daysList (){
Echo '<tr> ';
// Output Space (the space should be blank before the first day of October January)
For ($ j = 0; $ j <$ this-> start_weekday; $ j ++)
Echo '<td> </td> ';
For ($ k = 1; $ k <= $ this-> days; $ k ++ ){
$ J ++;
If ($ k = date ('D '))
Echo '<td class = "fontb">'. $ k. '</td> ';
Else
Echo '<td>'. $ k. '</td> ';
If ($ j % 7 = 0)
Echo '</tr> <tr> ';
}
// A few Spaces
While ($ j % 7! = 0 ){
Echo '<td> </td> ';
$ J ++;
}
Echo '</tr> ';
}
Private function prevYear ($ year, $ month ){
$ Year = $ year-1;
If ($ year <1970)
$ Year = 1970;
Return "year = {$ year} & month = {$ month }";
}
Private function prevMonth ($ year, $ month ){
If ($ month = 1 ){
$ Year = $ year-1;
If ($ year <1970)
$ Year = 1970;
$ Month = 12;
} Else {
$ Month --;
}
Return "year = {$ year} & month = {$ month }";
}
Private function nextYear ($ year, $ month ){
$ Year = $ year + 1;
If ($ year> 2038)
$ Year = 2038;
Return "year = {$ year} & month = {$ month }";
}
Private function nextMonth ($ year, $ month ){
If ($ month = 12 ){
$ Year ++;
If ($ year> 2100)
$ Year = 2100;
$ Month = 1;
} Else {
$ Month ++;
}
Return "year = {$ year} & month = {$ month }";
}
Private function chageDate ($ url = ""){
Echo '<tr> ';
Echo '<td> <a href = "? '. $ This-> prevYear ($ this-> year, $ this-> month ). '"> '. '<'. '</a> </td> ';
Echo '<td> <a href = "? '. $ This-> prevMonth ($ this-> year, $ this-> month ). '"> '. '<'. '</a> </td> ';
Echo '<td colspan = "3"> ';
Echo '<form> ';
Echo '<select name = "year" onchange = "window. location = \''. $ url .'? Year = \ '+ this. options [selectedIndex]. value + \' & month = '. $ this-> month.' \ '"> ';
For ($ sy = 1970; $ sy <= 2100; $ sy ++ ){
$ Selected = ($ sy = $ this-> year )? "Selected ":"";
Echo '<option'. $ selected. 'value = "'. $ sy.'"> '. $ sy.' </option> ';
}
Echo '</select> ';
Echo '<select name = "month" onchange = "window. location = \''. $ url .'? Year = '. $ this-> year.' & month = \ '+ this. options [selectedIndex]. value "> ';
For ($ sm = 1; $ sm <= 12; $ sm ++ ){
$ Selected1 = ($ sm = $ this-> month )? "Selected ":"";
Echo '<option'. $ selected1. 'value = "'. $ sm.'"> '. $ sm.' </option> ';
}
Echo '</select> ';
Echo '</form> ';
Echo '</td> ';
Echo '<td> <a href = "? '. $ This-> nextYear ($ this-> year, $ this-> month ). '"> '. '> '. '</a> </td> ';
Echo '<td> <a href = "? '. $ This-> nextMonth ($ this-> year, $ this-> month ). '"> '. '> '. '</a> </td> ';
Echo '</tr> ';
}
}
?>
Test. php
Copy codeThe Code is as follows:
<Style>
Table {
Border: 1px solid #050;
}
. Fontb {
Color: white;
Background: blue;
}
Th {
Width: 30px;
}
Td, th {
Height: 30px;
Text-align: center;
}
Form {
Margin: 0px;
Padding: 0px;
}
</Style>
<? Php
Include "calendar. class. php ";
$ Calendar = new Calendar;
$ Calendar-> out ();
?>