php万年历

上图是效果,刚学php,写个万年历玩玩~

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>php万年历</title>
    <style>
        #dusk{
            text-align:center;
            padding-top:50px;
        }
        h1{
            text-align:center;
        }
        #check
        {
            500px;
            margin:0 auto;
            text-align:center;
        }
    </style>
</head>
<body>
<h1>php万年历</h1>
<?php
header("content-type:text/html;charset=utf-8");
function inputCheck()
{
    if(@preg_match('/d{1,4}/',$_GET['y'])==1)//正则表达式输入框判断,必须是1-4个数字,否则返回false
    {
        return TRUE;
    }
    else
    {
        echo "<center>";
        echo"<font id=inputError color='red' size=20px text-align=center>请输入正确年份!</font>";
        echo "</center>";
        return FALSE;
    }
}
if(!inputCheck())
{
    $year = date('Y');//获取当前年份
    $month = date('m');//获取当前月份
    $days = date('t',strtotime ( "$year-$month-1" ));//获取当前月份天数
    $week = date('w',strtotime ( "$year-$month-1"));//获取当前星期
    echo "<center>";
    echo "<h2>{$year}年{$month}月</h2>";
    echo "</center>";
}
else
{
   @$year = $_GET['y'] ? $_GET['y'] : date('Y');//如果有GET方法传过来的参数就用get方法获得年份,如果没有就用当前年份
   @$month = $_GET['m'] ? $_GET['m'] : date('m');
    $days = date('t',strtotime ( "$year-$month-1" ));
    $week = date('w',strtotime ( "$year-$month-1"));
    echo "<center>";
    echo "<h2>{$year}年{$month}月</h2>";
    echo "</center>";
}  
    echo "<center>";
    echo "<table width=700px border=2px>";//画表
        echo "<tr>";
        echo "<th>周日</th>";
        echo "<th>周一</th>";
        echo "<th>周二</th>";
        echo "<th>周三</th>";
        echo "<th>周四</th>";
        echo "<th>周五</th>";
        echo "<th>周六</th>";
        echo "</tr>";
        for ( $i = 1 - $week ; $i < $days ;)
        {
            echo "<tr>";
            for( $j = 0 ; $j < 7 ; $j++)
            {   
                if($i < 1 || $i > $days)
                {
                    if($j % 2 == 0)//隔一行换个颜色
                    {
                        echo "<td bgcolor=#ccc>&nbsp;</td>";
                    }
                    else
                    {
                        echo "<td>&nbsp;</td>";
                    }
                }
                else
                {
                    if($j %2 == 0)
                    {   
                        if($year==date('Y')&&$month==date('m')&&$i==date('d'))//当前日期用蓝色标注
                        {
                        echo "<td bgcolor=blue>{$i}</td>";
                        }
                        else
                        {
                        echo "<td bgcolor=#A9A9A9>{$i}</td>";
                        }
                    }
                    else
                    {    if($year==date('Y')&&$month==date('m')&&$i==date('d'))
                        {
                        echo "<td bgcolor=blue>{$i}</td>";
                        }
                        else
                        {
                        echo "<td>{$i}</td>";
                        }
                    }    
                }
                $i++;
            }
            echo "</tr>";
        }
        echo "</table>";
        if($month==1)//判断上一月和下一月
        {
            $preyear = $year -1;
            $premonth = 12;
        }
        else{
            $preyear = $year;
            $premonth = $month - 1;
        }
        if($month == 12)
        {
            $nextyear = $year + 1;
            $nextmonth = 1;
        }
        else
        {
            $nextyear = $year;
            $nextmonth = $month + 1;
        }
        $last_year = $year - 1;
        $next_year = $year + 1;
        $nowyear = date('Y');
        $nowmonth = date('m');
    echo "</center>";//下面的a标签
    echo "<div id=dusk>
            <a href='index.php?y={$last_year}&m={$month}'>上一年</a>
            &nbsp;
            <a href='index.php?y={$preyear}&m={$premonth}'>上一月</a>
            |
            <a href='index.php?y={$nextyear}&m={$nextmonth}'>下一月</a>
            &nbsp;
            <a href='index.php?y={$next_year}&m={$month}'>下一年</a>
            <br>
            <a href='index.php?y={$nowyear}&m={$nowmonth}'>回到当前月</a>
        </div>";//月份的下拉菜单
    echo "<div id=check><form action='' method='get'>
            <input type='text' name='y' size='5'>年
            <select id='' name='m'>
                <option value='1'>1</option>
                <option value='2'>2</option>
                <option value='3'>3</option>
                <option value='4'>4</option>
                <option value='5'>5</option>
                <option value='6'>6</option>
                <option value='7'>7</option>
                <option value='8'>8</option>
                <option value='9'>9</option>
                <option value='10'>10</option>
                <option value='11'>11</option>
                <option value='12'>12</option>
            
            </select>
            <input type='submit' name='sub' value='查询'>
            </form>
          </div>";
?>
</body>
</html>


原文地址:https://www.cnblogs.com/Duskcl/p/4331083.html