php Calender(日历)代码
代码如下:
1 <?php 2 /** 3 * 4 * 我的日历 5 * date_default_timezone_set date mktime 6 * @param int $year 7 * @param int $month 8 * @param string $timezone 9 * @author fc_lamp 10 * @blog: http://www.cnblogs.com/roucheng/ 11 */ 12 function myCalender($year = '', $month = '', $timezone = 'Asia/Shanghai') 13 { 14 15 date_default_timezone_set ( $timezone ); 16 $year = abs ( intval ( $year ) ); 17 $month = abs ( intval ( $month ) ); 18 19 //是否是32位机 20 if (is32()) 21 { 22 if ($year < 1970 or $year >= 2038) 23 { 24 $year = date ( 'Y' ); 25 } 26 } else 27 { 28 if ($year <= 0) 29 { 30 $year = date ( 'Y' ); 31 } 32 33 } 34 35 if ($month <= 0 or $month > 12) 36 { 37 $month = date ( 'm' ); 38 } 39 40 //上一年 41 $pretYear = $year - 1; 42 //上一月 43 $mpYear = $year; 44 $preMonth = $month - 1; 45 if ($preMonth <= 0) 46 { 47 $preMonth = 1; 48 $mpYear = $pretYear; 49 } 50 51 //下一年 52 $nextYear = $year + 1; 53 //下一月 54 $mnYear = $year; 55 $nextMonth = $month + 1; 56 if ($nextMonth > 12) 57 { 58 $nextMonth = 1; 59 $mnYear = $nextYear; 60 } 61 62 //日历头 63 $html = <<<HTML 64 <table width="500" border="1"> 65 <tr align="center"> 66 <td><a href="?y=$pretYear">上一年</a></td> 67 <td><a href="?y=$mpYear&m=$preMonth">上一月</a></td> 68 <td><a href="?">回到今天</a></td> 69 <td><a href="?y=$mnYear&m=$nextMonth">下一月</a></td> 70 <td><a href="?y=$nextYear">下一年</a></td> 71 </tr> 72 <tr align="center"> 73 <td colspan="5">{$year}年{$month}月</td> 74 </tr> 75 <tr> 76 <td colspan="5"> 77 <table width="100%" border="1"> 78 <tr align="center"> 79 <td style="background-color:#DAF0DD;">星期一</td> 80 <td style="background-color:#DAF0DD;">星期二</td> 81 <td style="background-color:#DAF0DD;">星期三</td> 82 <td style="background-color:#DAF0DD;">星期四</td> 83 <td style="background-color:#DAF0DD;">星期五</td> 84 <td style="background-color:#F60;color:#fff;font-weight: bold;">星期六</td> 85 <td style="background-color:#F60;color:#fff;font-weight: bold;">星期天</td> 86 </tr> 87 HTML; 88 89 $currentDay = date ( 'Y-m-j' ); 90 91 //当月最后一天 92 $lastday = date ( 'j', mktime ( 0, 0, 0, $nextMonth, 0, $year ) ); 93 94 //循环输出天数 95 $day = 1; 96 $line = ''; 97 while ( $day <= $lastday ) 98 { 99 $cday = $year . '-' . $month . '-' . $day; 100 101 //当前星期几 102 $nowWeek = date ( 'N', mktime ( 0, 0, 0, $month, $day, $year ) ); 103 104 if ($day == 1) 105 { 106 $line = '<tr align="center">'; 107 $line .= str_repeat ( '<td> </td>', $nowWeek - 1 ); 108 } 109 110 if ($cday == $currentDay) 111 { 112 $style = 'style="color:red;"'; 113 } else 114 { 115 $style = ''; 116 } 117 118 $line .= "<td $style>$day</td>"; 119 120 //一周结束 121 if ($nowWeek == 7) 122 { 123 $line .= '</tr>'; 124 $html .= $line; 125 $line = '<tr align="center">'; 126 } 127 128 //全月结束 129 if ($day == $lastday) 130 { 131 if ($nowWeek != 7) 132 { 133 $line .= str_repeat ( '<td> </td>', 7 - $nowWeek ); 134 } 135 $line .= '</tr>'; 136 $html .= $line; 137 138 break; 139 } 140 141 $day ++; 142 } 143 144 $html .= <<<HTML 145 </table> 146 </td> 147 </tr> 148 </table> 149 HTML; 150 return $html; 151 } 152 153 /** 154 * 155 * 检测是否是32位机 156 * @author fc_lamp 157 * @blog: fc-lamp.blog.163.com 158 */ 159 function is32() 160 { 161 $is32 = False; 162 if (strtotime ( '2039-10-10' ) === False) 163 { 164 $is32 = True; 165 } 166 return $is32; 167 }