php获取前秒分时天

/**
 * 获取 几秒前,几分钟前,几小时前,几天前
 * by hkshadow
 * 2011-04-01
 * Enter description here ...
 * @param $val this mktime() or date()
 */
function ToTime($val){
	$nows = date("Y-m-d H:i:s",mktime());
	//$nows = date("Y-m-d H:i:s",mktime()+8*60*60);
	$nows = strtotime($nows);
	if (strlen($val) >= 11){ 
		$show_time = strtotime($val);
		}else{
			$show_time = $val;
		}
	$overtime = $nows - $show_time;
	if ($overtime < 0 ){
		return $overtime."秒前";
	}else if ($overtime < 60){
		return $overtime."秒前";
	}else if ($overtime < 3600){
		return ceil($overtime/60)."分钟前";
	}else if ($overtime < 86400){
		return ceil($overtime/3600)."小时前";
	}else if ($overtime < 259200) {  //三天前
		return ceil($overtime/86400)."天前";
	}else{
		return $overtime;
	}
}

Leave a Reply

(will not be published)