Home  >  Article  >  Backend Development  >  php判断时间为多少秒前,多少分钟前,多少小时前

php判断时间为多少秒前,多少分钟前,多少小时前

WBOY
WBOYOriginal
2016-06-20 13:02:041692browse

现在很多网站将时间的显示都变得比较人性化,例如:某某6秒之前发表,某某在1分钟之前执行过 XX 动作等等。最近在做相关的东西于是乎也顺便自己写了一个,较为简单,仅供参考。

<p><?php</p>/*<br />*function:显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前<br />*timeInt:unix time时间戳<br />*format:时间显示格式<br />*/<br />function timeFormat($timeInt,$format='Y-m-d H:i:s'){<br />	if(empty($timeInt)||!is_numeric($timeInt)||!$timeInt){<br />		return '';<br />	}<br />	$d=time()-$timeInt;<br />	if($d<0){<br />		return '';<br />	}else{<br />		if($d<60){<br />			return $d.'秒前';<br />		}else{<br />			if($d<3600){<br />				return floor($d/60).'分钟前';<br />			}else{<br />				if($d<86400){<br />					return floor($d/3600).'小时前';<br />				}else{<br />					if($d<259200){//3天内<br />						return floor($d/86400).'天前';<br />					}else{<br />						return date($format,$timeInt);<br />					}<br />				}<br />			}<br />		}<br />	}<br />}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn