一个论坛中的时间转换函数 ,论坛中经常会有发帖时间的自动变动,比如:几秒前,几分钟前,今天几点几分发的帖子,昨天几点几分发的帖子,今年及大于今年的时间发的帖子,下面整理一个时间戳转换函数,特意发出来分享一下,也希望,coder朋友们可以提出一些宝贵的建议!
//时间对比
public function time_tran($the_time) {
$now_time = time();
$wee_hours = strtotime(date('Y-m-d',time()));
$yesterday = $wee_hours - 24*60*60;
$dur = $now_time - $the_time;
if ($dur < 0) {
return $the_time;
} else {
if ($dur < 60) {
return $dur . '秒前';
} else {
if ($dur < 3600) {
return floor($dur / 60) . '分钟前';
} else {
if ($the_time > $wee_hours) {
return "今天 ".date("H:i",$the_time);
} else {
if ($the_time > $yesterday) {// 昨天
return '昨天 '.date("H:i",$the_time);
} else {
if ($the_time > mktime(0,0,0,1,1,date('Y'))) { //今年
return date("m月d日 H:i",$the_time);
} else {
return date("Y年m月d日 H:i",$the_time);
}
}
}
}
}
}
}
举几个��: