Home >Backend Development >PHP Tutorial >Timeline development in php

Timeline development in php

不言
不言Original
2018-04-14 16:10:432021browse

The content shared with you in this article is about the development of timeline in php, which has certain reference value. Friends in need can refer to

Timeline development in php, which is displayed as "just now" , "5 minutes ago", "Yesterday 10:23", etc.

In fact, this has no technical content. Of course, just paste the code directly without any nonsense.
But it is still quite useful in actual development, for example Forum posts, scarves, etc. all have related applications

Copy code The code is as follows:

function tranTime($time) { 
$rtime = date("m-d H:i",$time); 
$htime = date("H:i",$time); 
$time = time() - $time; 
if ($time < 60) { 
$str = &#39;刚刚&#39;; 
} 
elseif ($time < 60 * 60) { 
$min = floor($time/60); 
$str = $min.&#39;分钟前&#39;; 
} 
elseif ($time < 60 * 60 * 24) { 
$h = floor($time/(60*60)); 
$str = $h.&#39;小时前 &#39;.$htime; 
} 
elseif ($time < 60 * 60 * 24 * 3) { 
$d = floor($time/(60*60*24)); 
if($d==1) 
$str = &#39;昨天 &#39;.$rtime; 
else 
$str = &#39;前天 &#39;.$rtime; 
} 
else { 
$str = $rtime; 
} 
return $str; 
}


The parameter $time in the function tranTime() must be Unix timestamp, if not please use strtotime() to convert it to Unix timestamp first. The above code is easy to understand at a glance, so there is no need to elaborate further.
Call the function and output directly:

Copy code The code is as follows:

$times="1286861696 "; 
echo tranTime($times);

Related recommendations:

php time How to use stamp

Detailed explanation of PHP timestamp function

PHP timestamp and date conversion example sharing

The above is the detailed content of Timeline development in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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