Home >Backend Development >PHP Tutorial >Timeline development in php
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 = '刚刚'; } elseif ($time < 60 * 60) { $min = floor($time/60); $str = $min.'分钟前'; } elseif ($time < 60 * 60 * 24) { $h = floor($time/(60*60)); $str = $h.'小时前 '.$htime; } elseif ($time < 60 * 60 * 24 * 3) { $d = floor($time/(60*60*24)); if($d==1) $str = '昨天 '.$rtime; else $str = '前天 '.$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:
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!