A relatively good php calendar code
Copy codeThe Code is as follows:
<? Php
/**
* 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 = '<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 codeThe 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;
?> Bytes
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>