首页 > 代码库 > PHP日历的算法

PHP日历的算法

 1 <?php 2 if (function_exists(‘date_default_timezone_set‘)) { 3     date_default_timezone_set(‘Asia/Chongqing‘); 4 } 5 $date = isset($_GET[‘date‘]) ? $_GET[‘date‘] : date(‘Y-m-d‘); 6 $date = getdate(strtotime($date)); 7 $end = getdate(mktime(0, 0, 0, $date[‘mon‘] + 1, 1, $date[‘year‘]) - 1); 8 $start = getdate(mktime(0, 0, 0, $date[‘mon‘], 1, $date[‘year‘])); 9 $pre = date(‘Y-m-d‘, $start[0] - 1);10 $next = date(‘Y-m-d‘, $end[0] + 86400);11 $html = ‘<table border="1">‘;12 $html .= ‘<tr>‘;13 $html .= ‘<td><a href="http://www.mamicode.com/‘ . $PHP_SELF . ‘?date=‘ . $pre . ‘">-</a></td>‘;14 $html .= ‘<td colspan="5">‘ . $date[‘year‘] . ‘;‘ . $date[‘month‘] . ‘</td>‘;15 $html .= ‘<td><a href="http://www.mamicode.com/‘ . $PHP_SELF . ‘?date=‘ . $next . ‘">+</a></td>‘;16 $html .= ‘</tr>‘;17 $arr_tpl = array(0 => ‘‘, 1 => ‘‘, 2 => ‘‘, 3 => ‘‘, 4 => ‘‘, 5 => ‘‘, 6 => ‘‘);18 $date_arr = array();19 $j = 0;20 for ($i = 0; $i < $end[‘mday‘]; $i++) {21     if (!isset($date_arr[$j])) {22         $date_arr[$j] = $arr_tpl;23     }24     $date_arr[$j][($i+$start[‘wday‘])%7] = $i+1;25     if ($date_arr[$j][6]) {26         $j++;27     }28 }29 foreach ($date_arr as $value) {30     $html .= ‘<tr>‘;31     foreach ($value as $v) {32         if ($v) {33             if ($v == $date[‘mday‘]) {34                 $html .= ‘<td><b>‘ . $v . ‘</b></td>‘;35             } else {36                 $html .= ‘<td>‘ . $v . ‘</td>‘;37             }38         } else {39             $html .= ‘<td> </td>‘;40         }41     }42     $html .= ‘</tr>‘;43 }44 $html .= ‘</table>‘;45 echo $html;46 ?>

 

PHP日历的算法