Home >php教程 >PHP源码 >来自腾讯微博iweibo2.0更友好的时间格式如“3分钟前”

来自腾讯微博iweibo2.0更友好的时间格式如“3分钟前”

PHP中文网
PHP中文网Original
2016-05-25 17:13:101228browse

php代码

/**
 * timestamp转换成显示时间格式
 * @param $timestamp
 * @return unknown_type
 */
function tTimeFormat($timestamp)
{
	$curTime = time();
	$space = $curTime - $timestamp;
	//1分钟
	if($space < 60)
	{
		$string = "刚刚";
		return $string;
	}
	elseif($space < 3600) //一小时前
	{
		$string = floor($space / 60) . "分钟前";
		return $string;
	}
	$curtimeArray = getdate($curTime);
	$timeArray = getDate($timestamp);
	if($curtimeArray[&#39;year&#39;] == $timeArray[&#39;year&#39;])
	{
		if($curtimeArray[&#39;yday&#39;] == $timeArray[&#39;yday&#39;])
		{
			$format = "%H:%M";
			$string = strftime($format, $timestamp);
			return "今天 {$string}";
		}
		elseif(($curtimeArray[&#39;yday&#39;] - 1) == $timeArray[&#39;yday&#39;])
		{
			$format = "%H:%M";
			$string = strftime($format, $timestamp);
			return "昨天 {$string}";
		}
		else
		{
			$string = sprintf("%d月%d日 %02d:%02d", $timeArray[&#39;mon&#39;], $timeArray[&#39;mday&#39;], $timeArray[&#39;hours&#39;], 
			$timeArray[&#39;minutes&#39;]);
			return $string;
		}
	}
	$string = sprintf("%d年%d月%d日 %02d:%02d", $timeArray[&#39;year&#39;], $timeArray[&#39;mon&#39;], $timeArray[&#39;mday&#39;], 
	$timeArray[&#39;hours&#39;], $timeArray[&#39;minutes&#39;]);
	return $string;
}

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
Previous article:PHP EntityNext article:Zend Framework Feed