php计算几天前,几小时前,几分钟前

php计算几天前,几小时前,几分钟前的方法

<?php
date_default_timezone_set('Asia/Shanghai');

function formatTime($date) {
    $timer = strtotime($date);
    $diff = time() - $timer;
    $day = floor($diff / 86400);
    $hour = floor($diff / 3600);
    $min = floor($diff / 60);
    if($day > 0) {
        return $day.'天前';
    }
    if($hour>0){
        return $hour.'小时前';
    }
    if($min>0){
        return $min.'分钟前';
    }
    return '刚刚';
}

$str = '2019-08-04 12:01:19';
echo formatTime($str);

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: