Home >php教程 >PHP源码 >PHP formatting time, minutes ago, hours ago

PHP formatting time, minutes ago, hours ago

大家讲道理
大家讲道理Original
2016-11-11 09:38:091988browse

<?php  
//格式化时间,几分钟前,几小时前  
function format_date($time){  
    $t = time()-$time;  
    $f = array(  
        &#39;31536000&#39;=>&#39;年&#39;,  
        &#39;2592000&#39;=>&#39;个月&#39;,  
        &#39;604800&#39;=>&#39;星期&#39;,  
        &#39;86400&#39;=>&#39;天&#39;,  
        &#39;3600&#39;=>&#39;小时&#39;,  
        &#39;60&#39;=>&#39;分钟&#39;,  
        &#39;1&#39;=>&#39;秒&#39; 
    );  
    foreach($f as $k=>$v){  
        if(0 != $c = floor($t/(int)$k)){  
            return $c.$v.&#39;前&#39;;  
        }  
    }  
}  
echo format_date(&#39;1405959031&#39;);  
?>

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
Previous article:PHP file itselfNext article:PHP file itself