PHP开发PHP经验分享PHP实战

PHP万年历

2017-07-07  本文已影响85人  楊帥
    //获取当前年
    $year = $_GET['y']?$_GET['y']:date('Y');
    //获取当前月
    $month = $_GET['month']?$_GET['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 "<table width='700px' border='1px'>";
    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>";
    //下个月
    if($month == 12){
        $nextyear = $year+1;
        $nextmonth = 1;
    }else{
        $nextyear = $year;
        $nextmonth = $month+1;
    }
    //下个月日期
    $nextday = 1;
    //上个月
    if($month==1){
        $prevyear = $year-1;
        $prevmonth = 12;
    }else{
        $prevyear = $year;
        $prevmonth = $month-1;
    }
    //上个月日期
    $prevday = date('t',strtotime("{$prevyear}-{$prevmonth}-1"));
    //铺表格
    for($i=1-$week;$i<$days;){
        echo "<tr>";
            for($j=0;$j<7;$j++){
                if($i>$days){
                    echo "<td><font color='gray'>$nextday</font></td>";
                    $nextday++;
                }else if($i<1){
                    $day = $prevday+$i;
                    echo "<td><font color='gray'>$day</font></td>"; 
                }else{
                    echo "<td><font color='red'>{$i}</font></td>";
                }
                $i++;
            }
        echo "<tr>";
    }
    echo "</table>";
    echo "<h2><a href='index.php?y={$prevyear}&month={$prevmonth}'>上一月</a>|<a href='index.php?y={$nextyear}&month={$nextmonth}'>下一月</a></h2>";
    echo "<center>";            

效果图:


二维码.jpg

推荐下本人的微信公众号,本博客及其他方面的消息会定期同步到公众号上面!

上一篇下一篇

猜你喜欢

热点阅读