php - 将时间戳转换为之前

时间:2017-02-15 12:24:52

标签: php

我正在尝试将时间戳转换为之前。我这样做了

function ago2($time_ago) {
    $time_ago =  strtotime($time_ago) ? strtotime($time_ago) : $time_ago;
    $time  = time() - $time_ago;
    switch($time):
        case $time <= 60;
            return ($time == 1) ? '1 second ago' : $time.' seconds ago';
        case $time >= 60 && $time < 3600;
            return (round($time/60) == 1) ? '1 minute ago' : round($time/60).' minutes ago';
        case $time >= 3600 && $time < 86400;
            return (round($time/3600) == 1) ? '1 hour ago' : round($time/3600).' hours ago';
        case $time >= 86400 && $time < 604800;
            return (round($time/86400) == 1) ? '1 day ago' : round($time/86400).' days ago';
        case $time >= 604800 && $time < 2600640;
            return (round($time/604800) == 1) ? '1 week ago' : round($time/604800).' weeks ago';
        case $time >= 2600640 && $time < 31207680;
            return (round($time/2600640) == 1) ? '1 month ago' : round($time/2600640).' months ago';
        case $time >= 31207680;
            return (round($time/31207680) == 1) ? '1 year ago' : round($time/31207680).' years ago' ;
    endswitch;
}

效果很好。但是,如果日期早于1年,则只是将时间戳放入而不是转换为xxx之前。我在哪里做错了?...

0 个答案:

没有答案