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

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

PHP中文网
PHP中文网原创
2016-05-25 17:13:101227浏览

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;
}

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP Entity下一篇:Zend Framework Feed