Home >Backend Development >PHP Tutorial >PHP calculation posted a few minutes ago_PHP tutorial
The timeline developed in PHP is displayed as "just now", "5 minutes ago", "yesterday 10:23", etc. Friends in need can refer to it.
代码如下 | 复制代码 |
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 a Unix timestamp. If not, please use strtotime() to convert it to a Unix timestamp first
Call
The code is as follows
|
Copy code | ||||
http: //www.bkjia.com/PHPjc/631697.html