DateFormat PHP Class (php 处理日期)
代码:
<?phpclass 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->YearFu本文来源gaodai.ma#com搞##代!^码@网*搞gaodaima代码ll(); 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().'
';// 2echo $date->WeekDay().'
';// Tuesdayecho $date->Month().'
';// Augustecho $date->MonthShort().'
';// Augecho $date->MonthNum().'
';// 8echo $date->YearFull().'
';// 2011echo $date->Year().'
';// 11echo $date->MilitaryFull().'
';// 9:08:40echo $date->Military().'
';// 9:08echo $date->StandardFull().'
';// 9:08:40 amecho $date->Standard().'
';// 9:08 amecho $date->TextDate().'
';// August 2 2011echo $date->TextDateShort().'
';// Aug 2 11echo $date->NumDate().'
';// 8/2/2011echo $date->NumDateShort().'
';// 8/2/11echo $date->MonthDay().'
';// August 2echo $date->MonthDayShort().'
';// Aug 2echo $date->TimeSince(time()).'
';// Less than an hour ago...