Board logo

标题: 纯php实现简单的日历,可以选择日期 [打印本页]

作者: fangzhen    时间: 2010-1-2 10:27     标题: 纯php实现简单的日历,可以选择日期

<?php
$y=isset($_POST["select_y"])?$_POST["select_y"]:date("Y");
$m=isset($_POST["select_m"])?$_POST["select_m"]:date("m");
$today=date("Y").date("m").date("d");
?>
<!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=gb2312" />
<title>日历</title>
<style type="text/css">
body{
font-family:"宋体";
font-size:12px;
}
table{
border-width:0px 1px 1px 0px;
border-collapse:collapse;
border-style:solid;
border-color:#00CC00;
}
th,td{
border-width:1px 0px 0px 1px;
border-style:solid;
border-color: #00CC00;
text-align:center;
padding:10px;
}
.week6{
font-weight:bold;
color:#00FF00;
}
.week0,.week7{
font-weight:bold;
color:#FF0000;
}
.pre,.next{
color:#FF00CC;
}
.today{
background-color:#FF0000;
}
</style>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<select name="select_y" onchange="javascript:this.form.submit();">
<?php for($i=1995;$i<2015;$i++){ ?>
   <option value="<?php echo $i; ?>" <?php if($i==$y){echo 'selected="selected"';}?>><?php echo $i; ?></option>
<?php } ?>
</select>年
<select name="select_m" onchange="javascript:this.form.submit();">
<?php for($i=1;$i<13;$i++){ ?>
   <option value="<?php echo $i; ?>" <?php if($i==$m){echo 'selected="selected"';}?>><?php echo $i; ?></option>
<?php } ?>
</select>月
</form>
<table>
<tr>
   <th class="week0">日</th>
   <th>一</th>
   <th>二</th>
   <th>三</th>
   <th>四</th>
   <th>五</th>
   <th class="week6">六</th>
</tr>
<?php
$hao=1;
echo "<tr>";
for($i = getTS($y,$m-1) - (getWF($y,$m)-1); $i <= getTS($y,$m - 1); $i ++){
   echo "<td class=\"pre\"></td>";
}

for($i = 1; $i <= 7 - getWF($y, $m); $i ++){
   if($today == getFD($y, $m, $hao)){
     echo "<td class=\"today\">".$hao ++."</td>";
   }else{
     echo "<td class=\"week".getW($y, $m, $hao)."\">".$hao ++."</td>";
   }
}
echo "</tr>";
?>
<?php for($i = 2; $i < 7; $i ++){?>
<tr>
   <?php
   for($j = 0; $j < 7; $j ++){
  if($hao > getTS($y, $m)){
   echo "<td class=\"next\"></td>";
  }else{
   if($today == getFD($y, $m, $hao)){
    echo "<td class=\"today\">".$hao."</td>";
   }else{
    echo "<td class=\"week".$j."\">".$hao."</td>";
   }
  }
  $hao++;
   }
   ?>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php
function getWF($y,$m){//得到某年某月的1号星期几
 return idate("w",mktime(0,0,0,$m,1,$y));
}
function getTS($y,$m){//得到某年某月的总天数
 return idate("t",mktime(0,0,0,$m,1,$y));
}
function getW($y,$m,$d){ //得到某年某月的几号星期几
 return idate("w",mktime(0, 0, 0, $m,$d,$y));
}
function getFD($y,$m,$d){
 return date("Ymd",mktime(0, 0, 0, $m,$d,$y));
}
?>






欢迎光临 PHP开发笔记 (http://phpvi.com/) Powered by Discuz! 6.1.0