页面导航: 首页网络编程PHP编程php技巧 → 正文内容 php下的日历

php日历[测试通过]

发布:dxy 发布日期:2008-3-27 23:27:17 字体:[增加 减小] 类型:转载
一直喜欢php下的日历实现代码,测试效果不错

<?php
function calendar()
{
    if($_GET['ym'])
    {
        $year = substr($_GET['ym'],0,4);
        $month = substr($_GET['ym'],4,(strlen($_GET['ym'])-4));
        if($month>12)
        {
            $year += floor($month/12);
            $month = $month % 12;
        }
        if($year > 2030) $year = 2030;
        if($year < 1980) $year = 1980;
    }
    $year = isset($year) ? $year : date('Y');
    $month = isset($month) ? $month : date('n');
    if($year==date('Y') && $month==date('n')) $today = date('j');
    if($month-1 == 0)
        $prevmonth = ($year - 1)."12";
    else $prevmonth = $year.($month - 1);
    if($month+1 == 13)
        $nextmonth = ($year+1)."1";
    else $nextmonth = $year.($month+1);
    $prevyear = ($year - 1).$month;
    $nextyear = ($year + 1).$month;
    echo <<<VKN
        <table width="200" border="0" cellpadding="2" cellspacing="2">
  <tr>
    <td class="weekday"><a href="?ym=$prevyear"><<</a></td>
    <td class="normalday"><a href="?ym=$prevmonth"><</a></td>
    <td colspan="3" class="normalday">$year - $month</td>
    <td class="normalday"><a href="?ym=$nextmonth">></a></td>
    <td class="weekday"><a href="?ym=$nextyear">>></a></td>
  </tr>
  <tr>
    <td width="27" class="weekday">日</td>
    <td width="27" class="normalday">一</td>
    <td width="27" class="normalday">二</td>
    <td width="27" class="normalday">三</td>
    <td width="27" class="normalday">四</td>
    <td width="27" class="normalday">五</td>
    <td width="27" class="weekday">六</td>
  </tr>
VKN;
    $nowtime = mktime(0,0,0,$month,1,$year);//当月1号转为秒
    $daysofmonth = date(t,$nowtime);//当月天数
    $weekofbeginday = date(w,$nowtime);//当月第一天是星期几
    $weekofendday = date(w,mktime(0,0,0,$month+1,0,$year));//当月最后一天是星期几
    $daysofprevmonth = date(t,mktime(0,0,0,$month,0,$year));//上个月天数
    $count = 1;//计数
    //列出上月后几天
    for($i = 1 ; $i <= $weekofbeginday ; $i++)
        {
            echo     "<td class='othermonth'>".($daysofprevmonth-$weekofbeginday+$i)."</td>";
            $count++;
        }
    //当月全部
    for($i = 1 ; $i <= $daysofmonth ; $i++)
        {
            $css = ($count%7==0 || $count%7==1)?"weekday":"normalday";
            if($i == $today) $css .= "today";
            echo     "<td class='".$css."'>".$i."</td>";
            if($count%7==0) echo "</tr><tr>";
            $count++;
        }
    //下月前几天
    for ($i = 1;$i <= 6-$weekofendday;$i++)
        {
            echo     "<td class='othermonth'>".$i."</td>";
        }
    echo <<<VKN
          <tr>
    <td colspan="7"></td>
  </tr>
</table>
VKN;
}
?>
<!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>calendar</title>
<style type="text/css">
<!--
.weekday {
    font-size: 9pt;
    color: #FF0000;
    text-align: center;
}
.normalday {
    font-size: 9pt;
    color: #000000;
    text-align: center;
}
.weekdaytoday {
    font-size: 9pt;
    color: #FF0000;
    text-align: center;
    background-color: #FFD9D9;
    font-weight: bold;
}
.normaldaytoday {
    font-size: 9pt;
    color: #000000;
    text-align: center;
    background-color: #DDDDDD;
    font-weight: bold;
}
.othermonth {
    font-size: 9pt;
    font-style: italic;
    color: #999999;
    text-align: center;
}
-->
</style>
</head>
<body>
<?php calendar();?>
</body>
</html>
Tags:php 日历
百度中搜索更多的关于php日历[测试通过]内容,或者用Google搜索相关更多
浏览次数:载入中... 打印本文关闭本文返回首页

文章评论

共有 位脚本之家网友发表了评论我来说两句

同 类 文 章
最 近 更 新
热 点 排 行