In many cases, in order to show the timeliness of information, the time is usually displayed as "just now", "5 minutes ago", "3 hours ago", etc. instead of printing the time directly. For example, Weibo and SNS applications use this function for the longest time. The time format generally stored in the database is Unix timestamp, so here is a PHP function that converts Unix timestamp into timeline display.
The function is relatively simple, and it is easy to understand just by looking at the code.
Copy code The code is as follows:
date_default_timezone_set('PRC');
$ date = "1351836000";
echo tranTime($date);
function transfer_time($time)
{
$rtime = date("m-d H:i",$time);
$htime = date("H:i",$time);
$time = time() - $time;
if ($time < 60)
{
$str = 'Just';
}
elseif ($time < 60 * 60)
{
$min = floor($time/60);
$str = $min.'minutes before';
}
elseif ($time < 60 * 60 * 24)
{
$h = floor($time/(60*60));
$str = $h.'hours ago'.$htime;
}
elseif ($time < 60 * 60 * 24 * 3)
{
$d = floor($time/(60* 60*24));
if($d==1)
$str = 'yesterday'.$rtime;
else
$str = 'the day before yesterday'.$rtime;
}
else
{
$str = $rtime;
}
return $str;
}
?>
Note the function The parameter $time in transfer_time() must be a Unix timestamp. If not, please use strtotime() to convert it to a Unix timestamp
http://www.bkjia.com/PHPjc/327435.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327435.htmlTechArticleIn many occasions, in order to show the timeliness of information, the time is generally displayed as "just", "5 minutes" "Ago", "3 hours ago", etc. instead of printing the time directly. For example, Weibo...