今天贡献一个简单的计算时间差的方法,主要用于,新闻的时间显示
例如:刚刚,一个小时前,一天前,一周前等
/**
* 得到时间差
* @param datetime $t xxxx-xx-xx xx:xx:xx
*/
function getTimeSpan($t){
$t = strtotime($t);
$ct = time();
$d = ceil((intval($ct)-intval($t))/3600);
$s = ;
if($d<24){
$s = $d.小时前;
}elseif($d>=24 && $d<48){
$s = 1天前;
}elseif($d>=48 && $d<72){
$s = 2天前;
}elseif($d>=72 && $d<96){
$s = 3天前;
}elseif($d>=96 && $d<120){
$s = 4天前;
}elseif($d>=120 && $d<144){
$s = 5天前;
}elseif($d>=144 && $d<168){
$s = 6天前;
}elseif($d>=168){
$s = 1周前;
}
return $s;
}