Php simple calendar program code. PHP provides the date () function, which provides a variety of date processing functions. There are two types of data to be obtained: The first is the total number of days of the month, and the second is the date () function provided by the PHP where the first day of the month is located, this function provides rich date processing functions. There are two types of data to be obtained: The first is the total number of days of the month, and the second is the number of days of the week in which the first day of the month is located. The number indicates 0 (indicating Sunday) to 6 (Saturday ).
The date () function can easily obtain the above data.
The code is as follows: |
|
$ Month = $ _ GET ['M']? $ _ GET ['M']: date ('N '); $ Year = $ _ GET ['Y']? $ _ GET ['Y']: date ('Y '); $ Start_week = date ('W', mktime (0, 0, 0, $ month, 1, $ year )); $ Day_num = date ('t', mktime (0, 0, 0, $ month, 1, $ year )); $ End = false; ?>
$ J = 1; While ($ j <= $ day_num) { Echo"
If ($ week = 6 ){ Echo "nt
Sunday |
Monday |
Tuesday |
Wednesday |
Thursday |
Friday |
Saturday |
For ($ I = 0; $ I <$ start_week; $ I ++){Echo"
| ";}
$ J | ";$ Week = ($ start_week + $ J-1) % 7;
N ";If ($ j! = $ Day_num)Echo "t
Ntt ";Else $ end = true;}$ J ++;}While ($ week % 7! = 6){Echo"
| ";$ Week ++;}If (! $ End)Echo "n
";?>
|
Advanced class
Echo'
The code is as follows: |
|
Class Calendar { Private $ year; Private $ month; Private $ weeks = array ('day', 'yi', '2', '3', '4', '5', '6 '); Function _ construct ($ options = array ()){ $ This-> year = date ('Y '); $ This-> month = date ('M '); $ Vars = get_class_vars (get_class ($ this )); Foreach ($ options as $ key => $ value ){ If (array_key_exists ($ key, $ vars )){ $ This-> $ key = $ value; } } } Function display () { Echo'
';$ This-> showChangeDate ();$ This-> showWeeks ();$ This-> showDays ($ this-> year, $ this-> month );Echo'
'; } Private function showWeeks () { Echo' |
';Foreach ($ this-> weeks as $ title){Echo'
'. $ Title .' | ';}Echo'
';} Private function showDays ($ year, $ month){$ FirstDay = mktime (0, 0, 0, $ month, 1, $ year );$ StarDay = date ('W', $ firstDay );$ Days = date ('t', $ firstDay );
';For ($ I = 0; $ I <$ starDay; $ I ++ ){Echo'
| ';} For ($ j = 1; $ j <= $ days; $ j ++ ){$ I ++;If ($ j = date ('D ')){Echo'
'. $ J .' | ';} Else {Echo'
'. $ J .' | ';}If ($ I % 7 = 0 ){Echo'
';}} Echo'
';} Private function showChangeDate (){ $ Url = basename ($ _ SERVER ['php _ SELF ']); Echo'
';Echo'
PreYearUrl ($ this-> year, $ this-> month). '">'. '<'.' | ';Echo'
PreMonthUrl ($ this-> year, $ this-> month). '">'. '<'.' | ';Echo'
| ';Echo'
NextMonthUrl ($ this-> year, $ this-> month). '">'. '> '.' | ';Echo'
NextYearUrl ($ this-> year, $ this-> month). '">'. '> '.' | ';Echo'
';} Private function preYearUrl ($ year, $ month){$ Year = ($ this-> year <= 1970 )? 1970: $ year-1; Return 'Year = '. $ year.' & month = '. $ month;} Private function nextYearUrl ($ year, $ month){$ Year = ($ year> = 2038 )? 2038: $ year + 1; Return 'Year = '. $ year.' & month = '. $ month;} Private function preMonthUrl ($ year, $ month){If ($ month = 1 ){$ Month = 12;$ Year = ($ year <= 1970 )? 1970: $ year-1;} Else {$ Month --;} Return 'Year = '. $ year.' & month = '. $ month;} Private function nextMonthUrl ($ year, $ month){If ($ month = 12 ){$ Month = 1;$ Year = ($ year> = 2038 )? 2038: $ year + 1;} Else {$ Month ++;}Return 'Year = '. $ year.' & month = '. $ month;} }
Call method
The code is as follows: |
|
$ Params = array (); If (isset ($ _ GET ['Year']) & isset ($ _ GET ['month']) { $ Params = array ( 'Year' => $ _ GET ['Year'], 'Month' => $ _ GET ['month'], ); } $ Params ['URL'] = 'demo. php '; Require_once 'Calendar. class. php '; ?> Calendar demo
$ Cal = new Calendar ($ params ); $ Cal-> display (); ?>
|
Aggregate () function, which provides rich date processing functions. There are two types of data to be obtained: The first is the total number of days of the month, and the second is the first day of the month...