//時間轉換函數
function tranTime($time) {
$rtime = date("m-d H:i",$time);
$htime = date("H:i",$time);
$time = time() - $time;
if ($time < 60) {
$str = '剛剛';
}
elseif ($time < 60 * 60) {
$min = floor($time/60);
$str = $min.'分鐘前';
}
elseif ($time < 60 * 60 * 24) {
$h = floor($time/(60*60));
$str = $h.'小時前'.$htime;
}
elseif ($time < 60 * 60 * 24 * 3) {
$d = floor($time/(60*60*24));
if($d==1)
$str = '昨天'.$rtime;
else
$str = '前天'.$rtime;
}
else {
$str = $rtime;
}
return $str;
}
函數tranTime()中的參數$time必須為Unix時間戳,如果不是請先用strtotime()將其轉換成Unix時間戳。
調用函數,直接輸出:
$times="1286861696 ";
echo tranTime($times);
作者:aolinks 's Blog