各种时间显示处理

<?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->StandardFull().'<br>';
// 9:08:40 am

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

echo $date->TextDate().'<br>';
// August 2 2011

echo $date->TextDateShort().'<br>';
// Aug 2 11

echo $date->NumDate().'<br>';
// 8/2/2011

echo $date->NumDateShort().'<br>';
// 8/2/11

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

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

echo $date->TimeSince(time()).'<br>';
// Less than an hour ago...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值