I have always liked the calendar implementation code in php. a php calendar code with good test results
The code is as follows:
/**
* Calendar
*
* Copyright (c) 2007 by Chen Yixin (Deep Space). All rights reserved
* To contact the author write to {@ link mailto: shenkong@php.net}
* @ Author Chen Yixin (Deep Space)
*/
If (function_exists ('date _ default_timezone_set ')){
Date_default_timezone_set ('Asia/Chongqing ');
}
$ Date = isset ($ _ GET ['Date'])? $ _ GET ['Date']: date ('Y-m-D ');
$ Date = getdate (strtotime ($ date ));
$ End = getdate (mktime (0, 0, 0, $ date ['mon'] + 1, 1, $ date ['Year'])-1 );
$ Start = getdate (mktime (0, 0, 0, $ date ['mon'], 1, $ date ['Year']);
$ Pre = date ('Y-m-D', $ start [0]-1 );
$ Next = date ('Y-m-D', $ end [0] + 86400 );
$ Html ='
';$ Html. ='
';$ Html. ='
| - | ';$ Html. ='
'. $ Date ['Year'].'; '. $ date ['month'].' | ';$ Html. ='
+ | ';$ Html. ='
';$ Arr_tpl = array (0 => '', 1 =>'', 2 => '', 3 =>'', 4 => '', 5 => '', 6 => '');$ Date_arr = array ();$ J = 0;For ($ I = 0; $ I <$ end ['mday']; $ I ++ ){If (! Isset ($ date_arr [$ j]) {$ Date_arr [$ j] = $ arr_tpl;}$ Date_arr [$ j] [($ I + $ start ['wday']) % 7] = $ I + 1;If ($ date_arr [$ j] [6]) {$ J ++;}}Foreach ($ date_arr as $ value ){$ Html. ='
';Foreach ($ value as $ v ){If ($ v ){If ($ v = $ date ['mday']) {$ Html. ='
| '. $ V .' | ';} Else {$ Html. ='
'. $ V .' | ';}} Else {$ Html. ='
| ';}}$ Html. ='
';}$ Html. ='
';
Echo $ html;
?>
Php calendar code 2
The code is as follows:
/**
* Calendar
*/
If (function_exists ('date _ default_timezone_set ')){
Date_default_timezone_set ('Asia/Chongqing ');
}
$ Date = isset ($ _ GET ['Date'])? $ _ GET ['Date']: date ('Y-m-D ');
$ Date = getdate (strtotime ($ date ));
$ End = getdate (mktime (0, 0, 0, $ date ['mon'] + 1, 1, $ date ['Year'])-1 );
$ Start = getdate (mktime (0, 0, 0, $ date ['mon'], 1, $ date ['Year']);
$ Pre = date ('Y-m-D', $ start [0]-1 );
$ Next = date ('Y-m-D', $ end [0] + 86400 );
$ Html ='
Align = "center" style = "line-height: 150%; font-family: Verdana,; font-size: 12px;"> ';$ Html. ='
';$ Html. ='
| - | ';$ Html. ='
'. $ Date ['Year'].'; '. $ date ['month'].' | ';$ Html. ='
+ | ';$ Html. ='
';$ Arr_tpl = array (0 => '', 1 =>'', 2 => '', 3 =>'', 4 => '', 5 => '', 6 => '');$ Date_arr = array ();$ J = 0;For ($ I = 0; $ I <$ end ['mday']; $ I ++ ){If (! Isset ($ date_arr [$ j]) {$ Date_arr [$ j] = $ arr_tpl;}$ Date_arr [$ j] [($ I + $ start ['wday']) % 7] = $ I + 1;If ($ date_arr [$ j] [6]) {$ J ++;}}Foreach ($ date_arr as $ value ){$ Html. ='
';Foreach ($ value as $ v ){If ($ v ){If ($ v = $ date ['mday']) {$ Html. ='
| '. $ V .' | ';} Else {$ Html. ='
'. $ V .' | ';}} Else {$ Html. ='
| ';}}$ Html. ='
';}$ Html. ='
';
Echo $ html;
?> Bytes
The following is a good example. the system prompts errors and provides clear ideas.
The code is as follows:
Function calendar ()
{
If ($ _ GET ['ym'])
{
$ Year = substr ($ _ GET ['ym );
$ Month = substr ($ _ GET ['ym'], 4, (strlen ($ _ GET ['ym'])-4 ));
If ($ month> 12)
{
$ Year + = floor ($ month/12 );
$ Month = $ month % 12;
}
If ($ year> 2030) $ year = 2030;
If ($ year <1980) $ year = 1980;
}
$ Year = isset ($ year )? $ Year: date ('Y ');
$ Month = isset ($ month )? $ Month: date ('N ');
If ($ year = date ('Y') & $ month = date ('N') $ today = date ('J ');
If ($ month-1 = 0)
$ Prevmonth = ($ year-1). "12 ";
Else $ prevmonth = $ year. ($ month-1 );
If ($ month + 1 = 13)
$ Nextmonth = ($ year + 1). "1 ";
Else $ nextmonth = $ year. ($ month + 1 );
$ Prevyear = ($ year-1). $ month;
$ Nextyear = ($ year + 1). $ month;
Echo <
| < |
< |
$ Year-$ month |
> |
> |
| Day |
I |
II |
3. |
Thu |
V. |
Sat. |
VKN;$ Nowtime = mktime (0, 0, 0, $ month, 1, $ year); // convert the value 1 to the second of the current month$ Daysofmonth = date (t, $ nowtime); // Number of days in the current month$ Weekofbeginday = date (w, $ nowtime); // The Day of the week on the first day of the month$ Weekofendday = date (w, mktime (, 0, 0, $ month +, $ year); // The last day of the month is the day of the week.$ Daysofprevmonth = date (t, mktime (, 0, $ month, 0, $ year); // Number of days in the previous month$ Count = 1; // count// List the days after the previous monthFor ($ I = 1; $ I <= $ weekofbeginday; $ I ++){Echo"
| ". ($ Daysofprevmonth-$ weekofbeginday + $ I )." | ";$ Count ++;}// All for the current monthFor ($ I = 1; $ I <= $ daysofmonth; $ I ++){$ Css = ($ count % 7 = 0 | $ count % 7 = 1 )? "Weekday": "normalday ";If ($ I = $ today) $ css. = "today ";Echo"
". $ I ." | ";If ($ count % 7 = 0) echo"
";$ Count ++;}// A few days before next monthFor ($ I = 1; $ I <= 6-$ weekofendday; $ I ++){Echo"
| ". $ I ." | ";}Echo <
|
VKN;
}
?>
Calendar