A relatively good PHP calendar
Code Copy code The Code is as follows: <? PHP
/**
* Calendar
*
* Copyright (c) 2007 by Chen (Deep Space). All Rights Reserved
* To contact the author write to {@ link mailto: shenkong@php.net}
* @ Author Chen (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 = '<Table border = "1"> ';
$ Html. = '<tr> ';
$ Html. = '<TD> <a href = "'. $ php_self .'? Date = '. $ pre.' ">-</a> </TD> ';
$ HTML. = '<TD colspan = "5"> '. $ date ['Year']. ';'. $ date ['month']. '</TD> ';
$ Html. = '<TD> <a href = "'. $ php_self .'? Date = '. $ next.' "> + </a> </TD> ';
$ Html. = '</tr> ';
$ 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. = '<tr> ';
Foreach ($ value as $ v ){
If ($ v ){
If ($ v = $ date ['mday']) {
$ Html. = '<TD> <B>'. $ v. '</B> </TD> ';
} Else {
$ Html. = '<TD>'. $ v. '</TD> ';
}
} Else {
$ Html. = '<TD> </TD> ';
}
}
$ Html. = '</tr> ';
}
$ Html. = '</table> ';
Echo $ HTML;
?>
PHP calendar Code 2 Copy code The Code is as follows: <? PHP
/**
* 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 = '<Table width = "200" border = "1" cellspacing = "0" bordercolor = "#999999"
Align = "center" style = "line-Height: 150%; font-family: verdana,; font-size: 12px;"> ';
$ Html. = '<tr> ';
$ Html. = '<TD> <a href = "'. $ php_self .'? Date = '. $ pre.' ">-</a> </TD> ';
$ HTML. = '<TD colspan = "5"> '. $ date ['Year']. ';'. $ date ['month']. '</TD> ';
$ Html. = '<TD> <a href = "'. $ php_self .'? Date = '. $ next.' "> + </a> </TD> ';
$ Html. = '</tr> ';
$ 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. = '<tr> ';
Foreach ($ value as $ v ){
If ($ v ){
If ($ v = $ date ['mday']) {
$ Html. = '<TD> <B>'. $ v. '</B> </TD> ';
} Else {
$ Html. = '<TD>'. $ v. '</TD> ';
}
} Else {
$ Html. = '<TD> </TD> ';
}
}
$ Html. = '</tr> ';
}
$ Html. = '</table> ';
Echo $ HTML;
?>
The following is a good example. The system prompts errors and provides clear ideas.Copy codeThe Code is as follows: <? PHP
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 <vkn
<Table width = "200" border = "0" cellpadding = "2" cellspacing = "2">
<Tr>
<TD class = "weekday"> <a href = "? Ym = $ prevyear "> </a> </TD>
<TD class = "normalday"> <a href = "? Ym = $ prevmonth "> </a> </TD>
<TD colspan = "3" class = "normalday"> $ year-$ month </TD>
<TD class = "normalday"> <a href = "? Ym = $ nextmonth ">></A> </TD>
<TD class = "weekday"> <a href = "? Ym = $ nextyear ">></A> </TD>
</Tr>
<Tr>
<TD width = "27" class = "weekday"> day </TD>
<TD width = "27" class = "normalday"> 1 </TD>
<TD width = "27" class = "normalday"> 2 </TD>
<TD width = "27" class = "normalday"> 3 </TD>
<TD width = "27" class = "normalday"> 4 </TD>
<TD width = "27" class = "normalday"> 5 </TD>
<TD width = "27" class = "weekday"> 6 </TD>
</Tr>
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 month
For ($ I = 1; $ I <= $ weekofbeginday; $ I ++)
{
Echo "<TD class = 'othermonth'>". ($ daysofprevmonth-$ weekofbeginday + $ I). "</TD> ";
$ Count ++;
}
// All for the current month
For ($ I = 1; $ I <= $ daysofmonth; $ I ++)
{
$ CSS = ($ count % 7 = 0 | $ count % 7 = 1 )? "Weekday": "normalday ";
If ($ I = $ today) $ CSS. = "today ";
Echo "<TD class = '". $ CSS. "'>". $ I. "</TD> ";
If ($ count % 7 = 0) echo "</tr> <tr> ";
$ Count ++;
}
// A few days before next month
For ($ I = 1; $ I <= 6-$ weekofendday; $ I ++)
{
Echo "<TD class = 'othermonth'>". $ I. "</TD> ";
}
Echo <vkn
<Tr>
<TD colspan = "7"> </TD>
</Tr>
</Table>
Vkn;
}
?>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> calendar </title>
<Style type = "text/CSS">
<! --
. Weekday {
Font-size: 9pt;
Color: # ff0000;
Text-align: center;
}
. Normalday {
Font-size: 9pt;
Color: #000000;
Text-align: center;
}
. Weekdaytoday {
Font-size: 9pt;
Color: # ff0000;
Text-align: center;
Background-color: # ffd9d9;
Font-weight: bold;
}
. Normaldaytoday {
Font-size: 9pt;
Color: #000000;
Text-align: center;
Background-color: # dddddd;
Font-weight: bold;
}
. Othermonth {
Font-size: 9pt;
Font-style: italic;
Color: #999999;
Text-align: center;
}
-->
</Style>
</Head>
<Body>
<? PHP calendar ();?>
</Body>
</Html>