一、函数实现@H_403_1@实例1:@H_403_1@
403_1@function time_tran($the_time){@H_
403_1@ $now_time = date("Y-m-d H:i:s",time()+8*60*60);@H_
403_1@ $now_time = strtotime($now_time);@H_
403_1@ $show_time = strtotime($the_time);@H_
403_1@ $dur = $now_time - $show_time;@H_
403_1@ if($dur < 0){@H_
403_1@ return $the_time;@H_
403_1@ }else{@H_
403_1@ if($dur < 60){@H_
403_1@ return $dur.'秒前';@H_
403_1@ }else{@H_
403_1@ if($dur < 3600){@H_
403_1@ return floor($dur/60).'分钟前';@H_
403_1@ }else{@H_
403_1@ if($dur < 86400){@H_
403_1@ return floor($dur/3600).'小时前';@H_
403_1@ }else{@H_
403_1@ if($dur < 259200){//3天内@H_
403_1@ return floor($dur/86400).'天前';@H_
403_1@ }else{@H_
403_1@ return $the_time;@H_
403_1@ }@H_
403_1@ }@H_
403_1@}@H_
403_1@
@H_
403_1@实例2:@H_
403_1@
PHP@H_
403_1@function format_date($time){@H_
403_1@ $t=time()-$time;@H_
403_1@ $f=array(@H_
403_1@ '31536000'=>'年',@H_
403_1@ '2592000'=>'个月',@H_
403_1@ '604800'=>'星期',@H_
403_1@ '86400'=>'天',@H_
403_1@ '3600'=>'小时',@H_
403_1@ '60'=>'分钟',@H_
403_1@ '1'=>'秒'@H_
403_1@ );@H_
403_1@ foreach ($f as $k=>$v) {@H_
403_1@ if (0 !=$c=floor($t/(int)$k)) {@H_
403_1@ return $c.$v.'前';@H_
403_1@ }@H_
403_1@ }@H_
403_1@}@H_
403_1@?>
实例3:@H_403_1@
403_1@function formatTime($date) {@H_
403_1@$str = '';@H_
403_1@$timer = strtotime($date);@H_
403_1@$diff = $_SERVER['REQUEST_TIME'] - $timer;@H_
403_1@$day = floor($diff / 86400);@H_
403_1@$free = $diff % 86400;@H_
403_1@if($day > 0) {@H_
403_1@return $day."天前";@H_
403_1@}else{@H_
403_1@if($free>0){@H_
403_1@$hour = floor($free / 3600);@H_
403_1@$free = $free % 3600;@H_
403_1@if($hour>0){@H_
403_1@return $hour."小时前";@H_
403_1@}else{@H_
403_1@if($free>0){@H_
403_1@$min = floor($free / 60);@H_
403_1@$free = $free % 60;@H_
403_1@if($min>0){@H_
403_1@return $min."分钟前";@H_
403_1@}else{@H_
403_1@if($free>0){@H_
403_1@return $free."秒前";@H_
403_1@}else{@H_
403_1@return '刚刚';@H_
403_1@}@H_
403_1@}@H_
403_1@}else{@H_
403_1@return '刚刚';@H_
403_1@}@H_
403_1@}@H_
403_1@}else{@H_
403_1@return '刚刚';@H_
403_1@}@H_
403_1@}@H_
403_1@}@H_
403_1@
实例4:@H_403_1@
403_1@function time_tran($the_time){@H_
403_1@$now_time = date("Y-m-d H:i:s",time()+8*60*60); @H_
403_1@$now_time = strtotime($now_time);@H_
403_1@$show_time = strtotime($the_time);@H_
403_1@$dur = $now_time - $show_time;@H_
403_1@if($dur < 0){@H_
403_1@return $the_time; @H_
403_1@}else{@H_
403_1@if($dur < 60){@H_
403_1@ return $dur.'秒前'; @H_
403_1@}else{@H_
403_1@ if($dur < 3600){@H_
403_1@ return floor($dur/60).'分钟前'; @H_
403_1@ }else{@H_
403_1@ if($dur < 86400){@H_
403_1@ return floor($dur/3600).'小时前'; @H_
403_1@ }else{@H_
403_1@ if($dur < 259200){//3天内@H_
403_1@ return floor($dur/86400).'天前';@H_
403_1@ }else{@H_
403_1@ return $the_time; @H_
403_1@ }@H_
403_1@ }@H_
403_1@ }@H_
403_1@}@H_
403_1@}@H_
403_1@}@H_
403_1@
二、类的实现@H_403_1@
PHP@H_
403_1@/*@H_
403_1@* author: Solon Ring@H_
403_1@* time: 2011-11-02@H_
403_1@* 发博时间计算(年,月,日,时,分,秒)@H_
403_1@* $createtime 可以是当前时间@H_
403_1@* $gettime 你要传进来的时间@H_
403_1@*/
class Mygettime{
function __construct($createtime,$gettime) {@H_403_1@ $this->createtime = $createtime;@H_403_1@ $this->gettime = $gettime;@H_403_1@ }
function getSeconds()@H_403_1@ {@H_403_1@ return $this->createtime-$this->gettime;@H_403_1@ }
function getMinutes()@H_403_1@ {@H_403_1@ return ($this->createtime-$this->gettime)/(60);@H_403_1@ }
function getHours()@H_403_1@ {@H_403_1@ return ($this->createtime-$this->gettime)/(60*60);@H_403_1@ }
function getDay()@H_403_1@ {@H_403_1@ return ($this->createtime-$this->gettime)/(60*60*24);@H_403_1@ }
function getMonth()@H_403_1@ {@H_403_1@ return ($this->createtime-$this->gettime)/(60*60*24*30);@H_403_1@ }
function getYear()@H_403_1@ {@H_403_1@ return ($this->createtime-$this->gettime)/(60*60*24*30*12);@H_403_1@ }
function index()@H_403_1@ {@H_403_1@ if($this->getYear() > 1)@H_403_1@ {@H_403_1@ if($this->getYear() > 2)@H_403_1@ {@H_403_1@ return date("Y-m-d",$this->gettime);@H_403_1@ exit();@H_403_1@ }@H_403_1@ return intval($this->getYear())." 年前";@H_403_1@ exit();@H_403_1@ }
if($this->getMonth() > 1)@H_403_1@ {@H_403_1@ return intval($this->getMonth())." 月前";@H_403_1@ exit();@H_403_1@ }
if($this->getDay() > 1)@H_403_1@ {@H_403_1@ return intval($this->getDay())." 天前";@H_403_1@ exit();@H_403_1@ }
if($this->getHours() > 1)@H_403_1@ {@H_403_1@ return intval($this->getHours())." 小时前";@H_403_1@ exit();@H_403_1@ }
if($this->getMinutes() > 1)@H_403_1@ {@H_403_1@ return intval($this->getMinutes())." 分钟前";@H_403_1@ exit();@H_403_1@ }
if($this->getSeconds() > 1)@H_403_1@ {@H_403_1@ return intval($this->getSeconds()-1)." 秒前";@H_403_1@ exit();@H_403_1@ }
}
}@H_403_1@//类的使用实例@H_403_1@/*@H_403_1@*@H_403_1@* 调用类输出方式@H_403_1@*@H_403_1@* $a = new Mygettime(time(),strtotime('-25 month'));@H_403_1@* echo iconv('utf-8','gb2312',$a->index())?iconv('utf-8',$a->index()):iconv('utf-8','当前');@H_403_1@*@H_403_1@*/@H_403_1@
原文链接:https://www.f2er.com/php/24832.html