php 时间格式化几天/几月/几分钟/几秒前

<?php
     function format_date($time){
        $t = time() - $time;
        $f = ['31536000' => '年','2592000' => '个月',
            '604800' => '星期','86400' => '天',
            '3600' => '小时','60' => '分钟','1' => '秒'];
        foreach ($f as $k => $v) {
            if (0 != $c = floor($t / (int)$k)) {
                return $c . $v . '前';
            }
        }
    }
?>

关注友儿不迷路

Last modification:December 17th, 2020 at 04:17 pm
如果觉得我的文章对你有用,请随意赞赏