PHP 显示人性化时间

PHP

来自 http://yanue.net/post-56.html

function formatTime($time)
{
        $t = time() - $time;
        $f = array(
                '31536000' => '年',
                '2592000' => '个月',
                '604800' => '星期',
                '86400' => '天',
                '3600' => '小时',
                '60' => '分钟',
                '1' => '秒'
        );
        foreach ($f as $k => $v) {
                if (0 != $c = floor($t / (int)$k)) {
                        $m = floor($t % $k);
                        foreach ($f as $x => $y) {
                                if (0 != $r = floor($m / (int)$x)) {
                                        return $c.$v.$r.$y.'前';
                                }
                        }
                        return $c.$v.'前';
                }
        }
}
// 示例
$times = time() - rand(1, 9999);  
echo formatTime($times)
评论数量: 0

11
点赞
684
浏览
0
评论

贡献 31