查看: 5206|回复: 0
打印 上一主题 下一主题

php参考代码==Calendar Class

[复制链接]
跳转到指定楼层
1#
发表于 2007-10-4 16:33:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
<?php
/**
* Calendar Class
*
* @author   Avenger <avenger@php.net>
* @version  $Id 2003-05-10 14:02:34 $
*/

class Calendar {

    function Calendar() {
    }
    function getDayNames() {
        return $this->dayNames;
    }
    function setDayNames($names) {
        $this->dayNames = $names;
    }
    function getMonthNames() {
        return $this->monthNames;
    }
    function setMonthNames($names) {
        $this->monthNames = $names;
    }
    function getStartDay() {
        return $this->startDay;
    }
    function setStartDay($day) {
        $this->startDay = $day;
    }
    function getStartMonth() {
        return $this->startMonth;
    }
    function setStartMonth($month) {
        $this->startMonth = $month;
    }
    function getCalendarLink($month, $year) {
        return "";
    }
    function getDateLink($day, $month, $year) {
        return "";
    }
    function getCurrentMonthView() {
        $d = getdate(time());
        return $this->getMonthView($d["mon"], $d["year"]);
    }
    function getCurrentYearView() {
        $d = getdate(time());
        return $this->getYearView($d["year"]);
    }
    function getMonthView($month, $year) {
        return $this->getMonthHTML($month, $year);
    }
    function getYearView($year) {
        return $this->getYearHTML($year);
    }
    function getDaysInMonth($month, $year) {
        if ($month < 1 || $month > 12) {
            return 0;
        }
        $d = $this->daysInMonth[$month - 1];

        if ($month == 2) {
            if ($year%4 == 0) {
                if ($year%100 == 0) {
                    if ($year%400 == 0) {
                        $d = 29;
                    }
                } else  {
                    $d = 29;
                }
            }
        }

        return $d;
    }
    function getMonthHTML($m, $y, $showYear = 1) {
        $s = "";

        $a = $this->adjustDate($m, $y);
        $month = $a[0];
        $year = $a[1];

        $daysInMonth = $this->getDaysInMonth($month, $year);
        $date = getdate(mktime(12, 0, 0, $month, 1, $year));

        $first = $date["wday"];
        $monthName = $this->monthNames[$month - 1];

        $prev = $this->adjustDate($month - 1, $year);
        $next = $this->adjustDate($month + 1, $year);

        if ($showYear == 1) {
            $prevMonth = $this->getCalendarLink($prev[0], $prev[1]);
            $nextMonth = $this->getCalendarLink($next[0], $next[1]);
        } else {
            $prevMonth = "";
            $nextMonth = "";
        }

        $header = $monthName . (($showYear > 0) ? " " . $year : "");

        $s .= "<table class=\"calendar\" width=\"98%\">\n";
        $s .= "<tr>\n";
        $s .= "<td align=\"center\" valign=\"top\">" . (($prevMonth == "") ? " " : "<a href=\"$prevMonth\"><<") . "</td>\n";
        $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\" colspan=\"5\">$header</td>\n";
        $s .= "<td align=\"center\" valign=\"top\">" . (($nextMonth == "") ? " " : "<a href=\"$nextMonth\">>>") . "</td>\n";
        $s .= "</tr>\n";

        $s .= "<tr>\n";
        $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay)%7] . "</td>\n";
        $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+1)%7] . "</td>\n";
        $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+2)%7] . "</td>\n";
        $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+3)%7] . "</td>\n";
        $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+4)%7] . "</td>\n";
        $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+5)%7] . "</td>\n";
        $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+6)%7] . "</td>\n";
        $s .= "</tr>\n";

        $d = $this->startDay + 1 - $first;
        while ($d > 1) {
            $d -= 7;
        }

        $today = getdate(time());

        while ($d <= $daysInMonth) {
            $s .= "<tr>\n";

        for ($i = 0; $i < 7; $i++) {
            $class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarToday" : "calendar";
            $s .= "<td class=\"$class\" align=\"right\" valign=\"top\">";
            if ($d > 0 && $d <= $daysInMonth) {
                $link = $this->getDateLink($d, $month, $year);
                $s .= (($link == "") ? $d : "<a href=\"$link\">$d");
            } else {
                $s .= " ";
            }
            $s .= "</td>\n";
            $d++;
        }
        $s .= "</tr>\n";
        }

        $s .= "</table>\n";

        return $s;
    }
    function getYearHTML($year) {
        $s = "";
        $prev = $this->getCalendarLink(0, $year - 1);
        $next = $this->getCalendarLink(0, $year + 1);

        $s .= "<table class=\"calendar\" border=\"0\">\n";
        $s .= "<tr>";
        $s .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($prev == "") ? " " : "<a href=\"$prev\"><<") . "</td>\n";
        $s .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->startMonth > 1) ? $year . " - " . ($year + 1) : $year) ."</td>\n";
        $s .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($next == "") ? " " : "<a href=\"$next\">>>") . "</td>\n";
        $s .= "</tr>\n";
        $s .= "<tr>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(0 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "</tr>\n";
        $s .= "<tr>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "</tr>\n";
        $s .= "<tr>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "</tr>\n";
        $s .= "<tr>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "</tr>\n";
        $s .= "</table>\n";

    return $s;
    }
    function adjustDate($month, $year) {
        $a = array();
        $a[0] = $month;
        $a[1] = $year;

        while ($a[0] > 12) {
            $a[0] -= 12;
            $a[1]++;
        }

        while ($a[0] <= 0) {
            $a[0] += 12;
            $a[1]--;
        }

    return $a;
    }
   
    var $startDay = 0;
    var $startMonth = 1;
    var $dayNames = array("日", "一", "二", "三", "四", "五", "六");
    var $monthNames = array("January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December");
    var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

} //End Class
?>
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表