PHP實現簡單的萬年曆(實例)

發布時間:2018年05月26日      浏覽次數:581 次
<?
session_start();
header("content-type:text/html; charset=utf-8");
$year = trim($_GET['y']);
$year= ( is_numeric($year) and strlen($year) == 4 ) ? $year : gmdate('Y', time()+8*3600);
$month = trim($_GET['m']);
if ( strlen($month) == 1 ) $month = "0".$month;
$month= ( is_numeric($month) and strlen($month) == 2 ) ? $month : gmdate('m', time()+8*3600);

$days=date('t',strtotime("$year-$month-01"));//獲取當前月有多少天
$week=date('w',strtotime("$year-$month-01"));//當前1号是星期幾

//實現上一(yī)月和上一(yī)年
if($month==1){
      $premonth = 12;
      $preyear = $year - 1;
}else{
      $premonth = $month-1;
      if ( strlen($premonth) == 1 ) $premonth = "0".$premonth;
      $preyear = $year;
}

//實現下(xià)一(yī)月和下(xià)一(yī)年
if($month==12){
      $nextmonth = "01";
      $nextyear = $year + 1;
}else{
      $nextmonth = $month + 1;
      if ( strlen($nextmonth) == 1 ) $nextmonth = "0".$nextmonth;
      $nextyear = $year;
}
?>
<table width="600" border="0" align="center" cellpadding="4" cellspacing="1">
<tr>
<td width="20%"><div align="center"></div></td>
<td>
       <div align="center">
              <a href="?y=<?=$preyear?>&m=<?=$premonth?>"><<</a>   
       <b><?=$year?>年<?=$month?>月</b>
          <a href="?y=<?=$nextyear?>&m=<?=$nextmonth?>">>></a>
       </div></td>
<td width="20%"><div align="center"><a href="?">今天</a></div></td>
</tr>
</table>
<table width="600" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#dddddd">
<tr>
<th bgcolor="#FFFFFF">日</th>
<th bgcolor="#FFFFFF">一(yī)</th>
<th bgcolor="#FFFFFF">二</th>
<th bgcolor="#FFFFFF">三</th>
<th bgcolor="#FFFFFF">四</th>
<th bgcolor="#FFFFFF">五</th>
<th bgcolor="#FFFFFF">六</th>
</tr>
<?
for($i=1-$week; $i<=$days;){
      echo '<tr>';
      for($j=0; $j<7; $j++){
            $td_bgcolor = 'bgcolor="#FFFFFF"';
            if ( gmdate("Ymd", time()+8*3600) == $year.$month.$i ) $td_bgcolor = 'bgcolor="#D7FFD7"';
            $td_color = '';
            if ( $j==0 or $j==6 ) $td_color = 'style=" color:red;"';
            
            if( $i>$days or $i<=0 ){
                  echo '<td height="60" '.$td_bgcolor.' '.$td_color.'> </td>';
            }else{
                  echo '<td height="60" '.$td_bgcolor.' '.$td_color.'>'.$i.'</td>';
            }
            $i++;
      }
      echo '</tr>';
}
?>
</table>
免責聲明:本站相關技術文章信息部分(fēn)來自網絡,目的主要是傳播更多信息,如果您認爲本站的某些信息侵犯了您的版權,請與我(wǒ)(wǒ)們聯系,我(wǒ)(wǒ)們會即時妥善的處理,謝謝合作!