PHP自身函数代码中,有一个date()方法,它的作用是可把时间戳格式化为可读性更好的日期和时间。我们想要实现遍历本月日期,以月历方式输出,就需要用到date()。其实方法肯定不止一个,我只是选了一种我认为比较简单的。并把它写成了函数的形式,以便直接使用。
代码:
function Beiu_MouthHtml($row_html = 'tr', $col_html = 'td', $now_html = 'class="active"'){ $date = date("Y-m-d"); $temp = explode("-", $date); $days = (int) date("t"); $week = array('日', '一', '二', '三', '四', '五', '六'); $arr = array(); $key = 0; $row = 0; $res = ""; for($i = 1; $i <= $days; $i++){ if($i == 1){ $str = $temp[0]."-".$temp[1]."-".$i; $key = (int) date("w", strtotime($str)); } $arr[$key] = $i; $key++; } $res .= '<'.$row_html.'>'; foreach($week as $v){ $res .= '<'.$col_html.'>'; $res .= $v; $res .= '</'.$col_html.'>'; } $res .= '</'.$row_html.'>'; for($n = 0; $n < 35; $n++){ if($n % 7 == 0){ $row = $row + 1; $res .= '<'.$row_html.'>'; } $res .= '<'.$col_html; if(isset($arr[$n])){ if($temp[2] == $arr[$n]){ $res .= ' '.$now_html; } $res .= '>'.$arr[$n]; } else{ $res .= '>'; } $res .= '</'.$col_html.'>'; if($n == ($row * 7 - 1)){ $res .= '</'.$row_html.'>'; } } return $res; }