php日历

<?php
$year=isset($_POST['select_year'])?$_POST[select_year]:date("Y");
$month=isset($_POST['select_month'])?$_POST[select_month]:date("n");
if($year==date('Y')&&$month==date('n')) {
         $today = date('j');
}
function getFirstDay($year,$month){//每个月的第一天是星期几.
     return date(w,mktime(0,0,0,$month,1,$year));
}

function getMaxDay($year,$month){//每个月的总天数
     return date(t,mktime(0,0,0,$month,1,$year));
}

function getMaxDayPreviousMonth($year,$month){//上个月的总天数
     return date(t,mktime(0,0,0,$month-1,1,$year));
}
if(isset($_POST["previousyear"])){
     $year--;
}else if(isset($_POST["nextyear"])){
     $year++;
    
}else if(isset($_POST["previousmonth"])){
               if($month==1){
                    $month=12;
                    $year--;
                    }
               else {
                    $month--;
                    }
                        
}else if(isset($_POST["nextmonth"])){
             if($month==12){
                    $month=1;
                    $year++;
                    }
               else {$month++;
                   
                   }
}

if($year<1980){$year=date("Y");}
if($year>2050){$year=date("Y");}

?>
<!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=utf-8" />
<title>我的日历</title>
<style type="text/css">
body {
     font-family:"Arial Black", Gadget, sans-serif;
     font-size:24px;
}
table {
     border-collapse:collapse;
     border:1px #ccc solid;
}
table th, table td {
     border:1px #ccc solid;
     padding:15px;
}
table tr th:first-child, table tr td:first-child {
     color:#F00;
}
table tr th:first-child+th+th+th+th+th+th, table tr th:first-child+td+td+td+td+td+td {
     color:#0f0;
}
.week0 {
     color:#f00;
}
.week6 {
     color:#0f0;
}
.today {
    background-color:#999;
}
.previous,.next{
     color:#CCC !important;
}
</style>


</head>
<body>
<form action="" id="form1" method="post" name="form1">
  <select name="select_year" onchange="javascript:this.form.submit();">
    <?php
for($i=1980;$i<=2050;$i++){
     if($i==$year){
          echo '<option value="'.$i.'"  selected="selected">'.$i.'</option>';
          }else{
               echo '<option  value="'.$i.'">'.$i.'</option>';
               }
     }
    

?>
  </select>
  年
  <select name="select_month" onchange="javascript:this.form.submit();">
    <?php
for($i=1;$i<13;$i++){
     if($i==$month){
          echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
          }else{
               echo '<option  value="'.$i.'">'.$i.'</option>';
               }
     }
    

?>
  </select>
  月
  <input type="submit" value="上一年"  name="previousyear"/>
  <input type="submit" value="上一月"  name="previousmonth"/>
  <input type="submit" value="下一月"  name="nextmonth"/>
   <input type="submit" value="下一年"  name="nextyear"/>

</form>
<table>
  <colgroup>
  <col class="week0" />
  <col span="5" />
  <col class="week6" />
  </colgroup>
  <tr>
    <th>日</th>
    <th>一</th>
    <th>二</th>
    <th>三</th>
    <th>四</th>
    <th>五</th>
    <th>六</th>
  </tr>

<?php
$w=getFirstDay($year,$month);
$maxday=getMaxDay($year,$month);


for($i=0;$i<=5;$i++){
     echo "<tr>";
    
     for($j=0;$j<=6;$j++){
     $day =((1-$w)+$j+$i*7);//.......
    
     if($day<=0){
          echo '<td class="previous">'.(getMaxDayPreviousMonth($year,$month)+$day).'</td>';
     }
    if($day>0&&$day<=$maxday){
          if($day==$today){
               $css="today";
               }else {$css="";
               }
          echo "<td class='".$css."' >".$day.'</td>';
     }
     if($day>$maxday){
          echo '<td class="next">'.($day-$maxday).'</td>';
     }
         
     }
    
     echo "<tr>";
}

  ?>
</table>
</body>
</html>

posted on 2011-12-21 15:06  blli  阅读(1171)  评论(0)    收藏  举报