首頁 php教程 PHP源码 一个日期类

一个日期类

May 25, 2016 pm 05:15 PM

一个日期类

<?php
//创建一个日期类
class Calendar
{
	var $startDay=0;
	var $startMonth=1;
	var $dayNames=array("<font color=&#39;red&#39; face=&#39;courier new&#39;>Sunday</font>",
	 					 "<font color=&#39;green&#39; face=&#39;courier new&#39;>Monday",
	 					 "<font color=&#39;green&#39; face=&#39;courier new&#39;>Tuesday",
						 "<font color=&#39;green&#39; face=&#39;courier new&#39;>Wednesday",
						 "<font color=&#39;green&#39; face=&#39;courier new&#39;>Thursday",
 						 "<font color=&#39;green&#39; face=&#39;courier new&#39;>Friday",
						 "<font color=&#39;red&#39; face=&#39;courier new&#39;>Saturday");
    var $monthNames=array("<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>January</b></font>",
	        "<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>February</b></font>",
			"<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>March</b></font>",
			"<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>April</b></font>",
			"<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>May</b></font>",
			"<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>June</b></font>",
            "<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>July</b></font>",
			"<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>August</b></font>",
			"<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>September</b></font>",
			"<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>October</b></font>",
			"<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>November</b></font>",
			"<font color=&#39;maroon&#39; size=4 face=&#39;courier new&#39;><b>December</b></font>");
    var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	function setDayNames($names)
	{
		$this->dayNames=$names;
	}
	function getDayNames()
	{
		return $this->dayNames;
	}
	function setMonthNames($names)
	{
		$this->monthNames=$names;
	}
	function getMonthNames()
	{
		return $this->monthNames;
	}

	function setStartDay($day)
	{
		$this->startDay=$day;
	}
	//获取一个开始日期的函数
	function getStartDay()
	{
		return $this->startDay;
	}
	function setStartMonth($month)
	{
		$this->startMonth=$month;
	}
	function getStartMonth()
	{
		return $this->startMonth;
	}

	function getCalendarLink($month,$year)
	{
		return "";
	}

	//声明一个获取日期链接的函数
	function getDateLink($day,$month,$year)
	{
		$link="counter.php?day=$day&month=$month&year=$year&fromcounter=1";
		return "$link";
	}
	//设置日期链接
	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;
	}
	//声明一个获取月份的getMonthHTML函数
	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[&#39;wday&#39;];
		$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) ? " <font color=&#39;orange&#39;><b>".$year : "</b></font>");

$s.="<table class=&#39;calendar&#39; border=4 cellspacing=4 cellpadding=4 align=center bordercolor=blue";
$s.="<tr>";
$s.="<td align=&#39;center&#39; valign=&#39;top&#39;>".(($prevMonth=="") ? " " : "<a href=&#39;$prevMonth&#39;>&lt;&lt;</a>")."</td>";
$s.="<td align=&#39;center&#39; valign=&#39;center&#39; class=&#39;calendarHeader&#39; colspan=&#39;5&#39;>$header</td>";
$s.="<td align=&#39;center&#39; valign=&#39;top&#39;>".(($nextMonth=="") ? " " : "<a href=&#39;$nextMonth&#39;>&gt;&gt;</a>")."</td>";
		$s.="</tr>";
		$s.="<tr>";
$s.="<td align=&#39;center&#39; width=70 class=&#39;calendarHeader&#39;>".$this->dayNames[($this->startDay)%7]."</td>";
$s.="<td align=&#39;center&#39; width=70 class=&#39;calendarHeader&#39;>".$this->dayNames[($this->startDay+1)%7]."</td>";
$s.="<td align=&#39;center&#39; width=70 class=&#39;calendarHeader&#39;>".$this->dayNames[($this->startDay+2)%7]."</td>";
$s.="<td align=&#39;center&#39; width=70 class=&#39;calendarHeader&#39;>".$this->dayNames[($this->startDay+3)%7]."</td>";
$s.="<td align=&#39;center&#39; width=70 class=&#39;calendarHeader&#39;>".$this->dayNames[($this->startDay+4)%7]."</td>";
$s.="<td align=&#39;center&#39; width=70 class=&#39;calendarHeader&#39;>".$this->dayNames[($this->startDay+5)%7]."</td>";
$s.="<td align=&#39;center&#39; width=70 class=&#39;calendarHeader&#39;>".$this->dayNames[($this->startDay+6)%7]."</td>";
		$s.="</tr>";

		/********************************有些不解*************************************************/
		$d=$this->startDay+1-$first;
		while($d>1)
		{
			$d-=7;
		}
		$today=getdate(time());
		while($d<=$daysInMonth)
		{
			$s.="<tr>";
			for($i=0;$i<7;$i++)
			{
$class=($year==$today[&#39;year&#39;] && $month==$today[&#39;mon&#39;] && 
$d==$today[&#39;mday&#39;]) ? "calenderToday" : "calendar";
				$s.="<td class=&#39;$class&#39; align=&#39;center&#39;>";
				if($d>0 && $d<=$daysInMonth)
				{
					$link=$this->getDateLink($d,$month,$year);
					$mtime=mktime();
					$dnow=date("d",$mtime);
					$mnow=date("m",$mtime);
					if($dnow==$d && $mnow==$month)
					{
						$s.=(($link=="") ? $d : "<b>
			<a href=&#39;$link&#39;><font face=&#39;courier new&#39; color=&#39;red&#39;>$d</font></a></b>");

					}
					else
					{
						$s.=(($link=="") ? $d : "<a href=&#39;$link&#39;>
					<font face=&#39;courier new&#39; color=&#39;blue&#39;>$d</font></a>");

					}

				}
				else
				{
					$s.=" ";
				}
				$s.="</td>";
				$d++;
			}
			$s.="</tr>";
		}
		/***************************************************************************************/
		$s.="</table>";
		return $s;
	}

	//声明一个获取年份的HTML
	function getYearHTML($year)
	{
		$s="";
		$prev=$this->getCalendarLink(0,$year-1);
		$next=$this->getCalendarLink(0,$year+1);
		$s.="<table class=&#39;calendar&#39; border=&#39;1&#39;>";
		$s.="<tr>";
		$s.="<td align=&#39;center&#39; valign=&#39;top&#39;>".(($prev=="") ? " " : 
		"<a href=&#39;$prev&#39;>&lt;&lt;</a>")."</td>";
		$s.="<td class=&#39;calendarHeader&#39; vlign=&#39;top&#39; align=&#39;center&#39;>".(($this->startMonth>1) ? 
		$year."-".($year+1) : $year)."</td>";
		$s.="<td align=&#39;center&#39; valign=&#39;top&#39;>".(($next=="") ? " " : 
		"<a href=&#39;$next&#39;>&gt;&gt;</a>")."</td>";
		$s.="</tr>";
		$s.="<tr>";
		$s.="<td class=&#39;calendar&#39; valign=&#39;top&#39;>".$this->getMonthHTML(0+$this->startMonth,$year,0)."
		</td>";
		$s .= "<td class=\"calendar\" valign=\"top\">" . 
		$this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . 
        $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>";
        $s .= "</tr>";
        $s .= "<tr>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . 
        $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . 
        $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>";
        $s .= "<td class=\"calendar\" valign=\"top\">" .
         $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>";
        $s .= "</tr>";
        $s .= "<tr>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . 
        $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . 
        $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . 
        $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>";
        $s .= "</tr>";
        $s .= "<tr>";
        $s .= "<td class=\"calendar\" valign=\"top\">" .
         $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . 
        $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . 
        $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>";
        $s .= "</tr>";
        $s .= "</table>";
        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;
	}

}

class MyCalendar extends Calendar
{
	function getCalendarLink($month,$year)
	{
		$s=getenv(&#39;SCRIPT_NAME&#39;);	//相当于$_SERVER[&#39;SCRIPT_NAME&#39;]
		return "$s?month=$month&year=$year";
	}
}




?>
登入後複製

                   

 以上就是一个日期类的内容,更多相关内容请关注PHP中文网(www.php.cn)!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1320
25
PHP教程
1269
29
C# 教程
1249
24