欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2023-12-25 10:43:51
...
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['year'] == $timeArray['year'])
	{
		if($curtimeArray['yday'] == $timeArray['yday'])
		{
			$format = "%H:%M";
			$string = strftime($format, $timestamp);
			return "今天 {$string}";
		}
		elseif(($curtimeArray['yday'] - 1) == $timeArray['yday'])
		{
			$format = "%H:%M";
			$string = strftime($format, $timestamp);
			return "昨天 {$string}";
		}
		else
		{
			$string = sprintf("%d月%d日 %02d:%02d", $timeArray['mon'], $timeArray['mday'], $timeArray['hours'], 
			$timeArray['minutes']);
			return $string;
		}
	}
	$string = sprintf("%d年%d月%d日 %02d:%02d", $timeArray['year'], $timeArray['mon'], $timeArray['mday'], 
	$timeArray['hours'], $timeArray['minutes']);
	return $string;
}

上一篇:

下一篇: