这篇文章主要为大家详细介绍了php 将unix时间戳输出类似20秒前,1天前等的格式,具有一定的参考价值,可以用来参考一下。
将unix时间戳输出为类似于 20秒前,1天前,3个月前这样的格式,php将unix时间戳替换成易于阅读的格式,感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
/**
* unix时间戳输出类似20秒前,1天前等的格式
*
* @param
* @arrange (512.笔记) www.q1010.com
**/
function user_friendly_date($timestamp, $echo = false, $dateFormat = 'm/d/Y H:i:s')
{
$ufdate = '';
$now = time();
$elapsed = $now - $timestamp;
if($elapsed <= 0) $ufdate = 'Now';
else if($elapsed == 1) $ufdate = "1 Second ago";
else if($elapsed < 60) $ufdate = "{$elapsed} Seconds ago";
else if($elapsed < 3600) //One hour in seconds
{
$mins = floor($elapsed/60);
$secs = $elapsed%60;
$disp = min(($secs <= 30 ? $mins : $mins + 1), 59);
$ufdate = "{$disp} Minute" . ($disp == 1 ? '' : 's') ." ago";
}
else if($elapsed < 86400) //One day in seconds
{
$hours = floor($elapsed/3600);
$mins = floor(($elapsed%3600)/60);
$disp = min(($mins <= 30 ? $hours : $hours + 1), 23);
$ufdate = "{$disp} Hour" . ($disp == 1 ? '' : 's') ." ago";
}
else if($elapsed < 604800) //One week in seconds
{
$days = floor($elapsed/86400);
$hours = floor(($elapsed%86400)/3600);
$disp = min(($hours <= 12 ? $days : $days + 1), 6);
$ufdate = "{$disp} Day" . ($disp == 1 ? '' : 's') ." ago";
}
else $ufdate = date($dateFormat, $timestamp);
if($echo) echo $ufdate;
return $ufdate;
}
//Usage
$now = time();
$tenSecsAgo = $now - 10;
$oneMinuteAgo = $now - 60;
$thirtyNineMinsAgo = $now - (39 * 60); //minutes * seconds
$elevenHoursAgo = $now - (11 * 60 * 60); //hours * minutes * seconds
$twoDaysAgo = $now - (2 * 24 * 60 * 60); //days * hours * minutes * seconds
echo "<p>\n";
echo user_friendly_date($tenSecsAgo), " <br>\n";
echo user_friendly_date($oneMinuteAgo), " <br>\n";
echo user_friendly_date($thirtyNineMinsAgo), " <br>\n";
echo user_friendly_date($elevenHoursAgo), " <br>\n";
echo user_friendly_date($twoDaysAgo), " <br>\n";
echo "</p>";
/*** 来自四海网(www.q1010.com) ***/
本文来自:http://www.q1010.com/173/392-0.html
注:关于php 将unix时间戳输出类似20秒前,1天前等的格式的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:时间戳
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。