php实现万年历 - 旭辉博客
TypechoJoeTheme

旭辉博客

PHP

php实现万年历

 <?php
header("content-type:text/html;charset=utf-8");
$y = date("Y");
$m = date("m");
$day = date("d");//1
$h = date("H");
$min = date("i");
$sec = date("s");
// 判断是否接收到year,如果接收不到,默认当前年
$year = isset($_GET['year'])?$_GET['year']:date("Y");
if(  (int)$year <1970 || (int)$year>=2038){
    $year = date("Y");
}
$month = isset($_GET['month'])?$_GET['month']:date("m");
if((int)$month<1 || (int)$month>12){
    $month = date("m");
}

// 1.这个月有几天?
$days = date("t",mktime(0,0,0,$month,1,$year));
// 2. 每个月1号是周几?
$w = date("w",mktime(0,0,0,$month,1,$year));
$premonth = $nextmonth = $month;
$preyear = $nextyear = $year;
if($month>1 && $month <12){ // 如果不是1月 也不是12 月
    $premonth--;   // 上一月-1
    $nextmonth++; // 下一月 +1
} // 如果当前是12月
else if($month == 12){
    $nextmonth = 1;  // 下一月就是1月
    $premonth--;    // 上一月就是当前月-1
    $nextyear++;    // 下一年就是当前年+1

}else if($month == 1){ // 如果当前是1月=
    $premonth = 12; // 上一月就是12月
    $nextmonth++;   // 下一月就是当前月+1
    $preyear--;     // 上一年就是当年-1
}
?>
<h3 align="center">
    <span><?php echo $y;?></span>-<span><?php echo $m;?></span>-<span><?php echo $day;?></span> <span><?php echo $h;?></span>:<span><?php echo $min;?></span>:<span><?php echo $sec;?></span>
</h3>
<?php
echo "<table border='1' width='400' align='center' cellpadding='0' cellspacing='0'>";

echo "<caption>
<h3>
<a href='?month=$month&year=".($year-1)."' >上一年</a>
 <a href='?month=$premonth&year=$preyear'>上一月</a> 
 {$year}年{$month}月
<a href='?month=$nextmonth&year=$nextyear'>下一月</a>
<a href='?month=$month&year=".($year+1)."'>下一年</a> 
</h3>
</caption>";
echo "<tr>
        <th>日</th>
        <th>一</th>
        <th>二</th>
        <th>三</th>
        <th>四</th>
        <th>五</th>
        <th>六</th>
        </tr>";
echo "<tr>";
// 输出空td
for($j=0;$j<$w;$j++){
    echo "<td></td>";
}
for($i=1;$i<=$days;$i++,$j++){
    if($j%7==0 && $j!=0){
        echo "</tr><tr>";
    }
    if($i == $day && $month == $m && $year == $y){
        echo "<td bgcolor='red' align='center' valign='center'>{$i}</td>";
    }else{
        echo "<td align='center' valign='center'>{$i}</td>";
    }

}
echo "</tr>";
echo "</table>";
?>
<script>
    var tds = document.querySelectorAll('td');
    var l = tds.length;
    for(var i=0;i<l;i++){
        tds[i].onclick = function () {
            // 所有的td恢复原状
            for(var j=0;j<l;j++){
                tds[j].style.border = "1px solid #000";
            }
            this.style.border = "1px solid #f00";
        }
    }
    function randColor(){
        var r = Math.floor(Math.random()*256)
        var g = Math.floor(Math.random()*256)
        var b = Math.floor(Math.random()*256)
        return "rgb("+r+","+g+","+b+")"
    }
    var spans = document.querySelectorAll('span');
    var l = spans.length;
    for(var i=0;i<l;i++){
        spans[i].style.color = randColor();
    }
    // 动态时间
    function show(){
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth()+1;
        var day = date.getDate();
        day = day>9?day:"0"+day;
        var hour = date.getHours();
        var min  = date.getMinutes();
        var sec = date.getSeconds();
        spans[0].innerText = year;
        spans[1].innerText = month;
        spans[2].innerText = day;
        spans[3].innerText = hour;
        spans[4].innerText = min;
        spans[5].innerText = sec;
        // var randcolor = randColor();
        for(var i=0;i<l;i++){
            spans[i].style.color = randColor();
        }
    }
    setInterval(show,1000);
</script>
赞(0)
评论 (0)
召唤看板娘