Home > php教程 > PHP源码 > body text

PHP formatting time, minutes ago, hours ago

大家讲道理
Release: 2016-11-11 09:38:09
Original
1985 people have browsed it

<?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;);  
?>
Copy after login

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template