方法1:

function ueTime($times) {
if ($times == '' || $times == 0) {
return false;
}
//完整時間戳
$strtotime = is_int($times) ? $times : strtotime($times);
$times_day = date('Y-m-d', $strtotime);
$times_day_strtotime = strtotime($times_day);
//今天
$nowdate_str = strtotime(date('Y-m-d'));
//精确的時間間隔(秒)
$interval = time() - $strtotime;
//今天的
if ($times_day_strtotime == $nowdate_str) {
//小于一分鐘
if ($interval < 60) {
$pct = sprintf("%d秒前", $interval);
}
//小于1小時
elseif ($interval < 3600) {
$pct = sprintf("%d分鐘前", ceil($interval / 60));
} else {
$pct = sprintf("%d小時前", floor($interval / 3600));
}
}
//昨天的
elseif ($times_day_strtotime == strtotime(date('Y-m-d', strtotime('-1 days')))) {
$pct = '昨天' . date('H:i', $strtotime);
}
//前天的
elseif ($times_day_strtotime == strtotime(date('Y-m-d', strtotime('-2 days')))) {
$pct = '前天' . date('H:i', $strtotime);
}
//一個月以内
elseif ($interval < (3600 * 24 * 30)) {
$pct = date('m月d日', $strtotime);
}
//一年以内
elseif ($interval < (3600 * 24 * 365)) {
$pct = date('m月d日', $strtotime);
}
//一年以上
else {
$pct = date('Y年m月d日', $strtotime);
}
return $pct;
}
方法2:
header("Content-type: text/html; charset=utf8");
date_default_timezone_set("Asia/Shanghai"); //設定時區
function time_tran($the_time) {
$now_time = date("Y-m-d H:i:s", time());
//echo $now_time;
$now_time = strtotime($now_time);
$show_time = strtotime($the_time);
$dur = $now_time - $show_time;
if ($dur < 0) {
return $the_time;
} else {
if ($dur < 60) {
return $dur . '秒前';
} else {
if ($dur < 3600) {
return floor($dur / 60) . '分鐘前';
} else {
if ($dur < 86400) {
return floor($dur / 3600) . '小時前';
} else {
if ($dur < 259200) {//3天内
return floor($dur / 86400) . '天前';
} else {
return $the_time;
}
}
}
}
}
}
echo time_tran("2014-7-8 19:22:01");
?>
方法3:
function wordTime($time) {
$time = (int) substr($time, 0, 10);
$int = time() - $time;
$str = '';
if ($int <= 2){
$str = sprintf('剛剛', $int);
}elseif ($int < 60){
$str = sprintf('%d秒前', $int);
}elseif ($int < 3600){
$str = sprintf('%d分鐘前', floor($int / 60));
}elseif ($int < 86400){
$str = sprintf('%d小時前', floor($int / 3600));
}elseif ($int < 2592000){
$str = sprintf('%d天前', floor($int / 86400));
}else{
$str = date('Y-m-d H:i:s', $time);
}
return $str;
}