penven

博客园 首页 新随笔 联系 订阅 管理

 

PHP 对于时间的过了多久的判断,几秒前,几分钟前,几小时前,$time = strtotime("2017-01-15 14:42:00");

 

$time = strtotime("2017-01-15 16:42:00");
echo ReckonTime($time);

function ReckonTime($time)
{
    $NowTime = time();
    if($time > $NowTime){ return false; }
    $TimePoor = $NowTime - $time;
    if ($TimePoor == 0) {
        $str = '一眨眼之间';
    } else if ($TimePoor < 60 && $TimePoor > 0) {
        $str = $TimePoor . '秒之前';
    } else if ($TimePoor >= 60 && $TimePoor <= 60 * 60) {
        $str = floor($TimePoor / 60) . '分钟前';
    } else if ($TimePoor > 60 * 60 && $TimePoor <= 3600 * 24) {
        $str = floor($TimePoor / 3600) . '小时前';
    } else if ($TimePoor > 3600 * 24 && $TimePoor <= 3600 * 24 * 7) {
        if (floor($TimePoor / (3600 * 24)) == 1) {
            $str = "昨天";
        } else if (floor($TimePoor / (3600 * 24)) == 2) {
            $str = "前天";
        } else {
            $str = floor($TimePoor / (3600 * 24)) . '天前';
        }
    } else if ($TimePoor > 3600 * 24 * 7) {
        $str = date("Y-m-d", $time);
    }
    return $str;
}

 

 

 

if($time > $NowTime){ return false; }
posted on 2017-01-15 14:44  penven  阅读(982)  评论(0编辑  收藏  举报