Home >Backend Development >PHP Tutorial > php时间轴函数 ,刚、1分钟前、1小时前、一天前

php时间轴函数 ,刚、1分钟前、1小时前、一天前

WBOY
WBOYOriginal
2016-06-13 13:04:241483browse

php时间轴函数 ,刚刚、1分钟前、1小时前、一天前

php常见时间处理函数:

time():返回当前的 Unix 时间戳?。

date():格式化一个本地时间/日期。

getdate():取得日期/时间信息。

mktime():正常日期转时间戳。mktime(0, 0, 0, 9, 18, 2011)

如下是一个时间轴处理函数

/**
	 * 时间格式化
	 */
	static function formatDate($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;  
	}
?
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