Home >Backend Development >PHP Tutorial >PHP calculation posted a few minutes ago_PHP tutorial

PHP calculation posted a few minutes ago_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:55:04906browse

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
 代码如下 复制代码

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

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

http://www.bkjia.com/PHPjc/631697.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631697.html
TechArticle
Timeline development in php, which shows "just now", "5 minutes ago", "yesterday 10:23" "Wait, friends in need can refer to it. The code is as follows Copy code function tranTime($time)...
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