日期:2014-05-17  浏览次数:20579 次

DateFormat PHP Class (php 处理日期)

代码:

<?php

class FormatDate
{
    var $theTime;
    function FormatDate($string)
    {
        	//Set constructor
	        $this->theTime = $string;
	}

	//Returns numerical day
	function Day() 	{	return date("j", $this->theTime); }
	
	//Returns weekday
	function WeekDay() 	{ 	return date("l", $this->theTime); }

	//Returns full month
	function Month()	{	return date("F", $this->theTime); }

	//Returns short-hand month
	function MonthShort() 	{ 	return date("M", $this->theTime); }

	//Numeric for month
	function MonthNum() 	{ 	return date("n", $this->theTime); }
	
	//Full 4 digit year
	function YearFull() 	{ 	return date("Y", $this->theTime); }

	//Short 2 digit year
	function Year() 	{ 	return date("y", $this->theTime); }

	//24 Hr with Seconds
	function MilitaryFull()	{	return date("G:i:s", $this->theTime); }

	//24 Hr without Seconds
	function Military()	{	return date("G:i", $this->theTime); }

	//Standard with seconds
	function StandardFull()	{	return date("g:i:s a", $this->theTime); }

	//Standard without seconds
	function Standard()	{	return date("g:i a", $this->theTime); }

	//Date & Month & Year Full
	function TextDate()	{	$string = $this->Month()." ".$this->Day()." ".$this->YearFull();
					return $string;
				}
	
	//Date & Month & Year Shorthand
	function TextDateShort(){	$string = $this->MonthShort()." ".$this->Day()." ".$this->Year();
					return $string;
				}

	//Numerical Date & Month & Year
	function NumDate()	{	$string = $this->MonthNum()."/".$this->Day()."/".$this->YearFull();
					return $string;
				}

	//Numerical Date & Month & Year Shorthand
	function NumDateShort()	{	$string = $this->MonthNum()."/".$this->Day()."/".$this->Year();
					return $string;
				}

	//Month & Day Full
	function MonthDay()	{	$string = $this->Month()." ".$this->Day();
					return $string;
				}

	//Month & Day Short
	function MonthDayShort(){	$string = $this->MonthShort()." ".$this->Day();
					return $string;
				}

	function TimeSince($old_stamp) {
		$difference = $this->theTime - $old_stamp;
		
		$loop = true;
		while($loop) {
			if(round($difference/3153600, 2) >= 1) { return "Over a year..."; }
			elseif(round($difference/2592000, 2) >= 2) { return "Over ".round($difference/2592000,0)." months ago..."; }	
			elseif(round($difference/2592000, 2) >= 1.20) { return "Over a month ago..."; }
			elseif(round($difference/604800, 2) >= 2) { return "Over ".round($difference/604800,0)." weeks ago.."; }
			elseif(round($difference/604800, 2) >= 1.20) { return "Over a week ago.."; }
			elseif(round($difference/86400, 2) >= 1.9) { return "Over a few days ago...";}
			elseif(round($difference/3600, 2) >= 3) { return "Just a few hours ago.."; }
			elseif(round($difference/3600, 2) >= 8) { return "About half a day ago..."; }
		
			elseif(round($difference/3600, 2) < 1) { return "Less than an hour ago..."; }
			elseif(round($difference/86400, 2) < 1.9) { return "About a day ago..."; }
			elseif(round($difference/86400, 2) < 6 ) { return "Less than a week ago..."; }
			elseif(round($difference/604800, 2) < 1.20) { return "About a week ago.."; }
			elseif(round($difference/2592000, 2) < 1.20) { return "About a month ago..."; }
			
			else{ return "Error"; }
			$loop = false;
		}		
		
	}
}
?

实例:

$date = new FormatDate(time());

echo $date->Day().'<br>';
// 2

echo $date->WeekDay().'<br>';
// Tuesday

echo $date->Month().'<br>';
// August

echo $date->MonthShort().'<br>';
// Aug

echo $date->MonthNum().'<br>';
// 8

echo $date->YearFull().'<br>';
// 2011

echo $date->Year().'<br>';
// 11

echo $date->MilitaryFull().'<br>';
// 9:08:40

echo $date->Military().'<br>';
// 9:08

echo $date