Home > php教程 > PHP源码 > body text

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

PHP中文网
Release: 2016-05-25 17:13:10
Original
1164 people have browsed it

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;
}
Copy after login

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!