PHP:几分钟前,几秒钟前:这是正确编码的吗?

时间:2010-08-12 18:30:59

标签: php

if($timestamp > time() - 60){
    // Count seconds ago
    $deff = time() - $timestamp;
    $seconds = $deff / 60;
    echo intval($seconds)." seconds ago";
} elseif($timestamp > time() - 3600){
    // Count minutes ago
    $deff = time() - $timestamp;
    $minuts = $deff / 60;
    echo intval($minuts)." minutes ago";
}

我认为分钟前是正确的,但不是秒?我应该怎么做“秒前”吧?

5 个答案:

答案 0 :(得分:6)

我使用类似于此的小功能,可能不是最好的,不会处理未来的时间,但如果您想要的只是过去,那就没问题了。

function ago($when) {
        $diff = date("U") - $when;

        // Days
        $day = floor($diff / 86400);
        $diff = $diff - ($day * 86400);

        // Hours
        $hrs = floor($diff / 3600);
        $diff = $diff - ($hrs * 3600);

        // Mins
        $min = floor($diff / 60);
        $diff = $diff - ($min * 60);

        // Secs
        $sec = $diff;

        // Return how long ago this was. eg: 3d 17h 4m 18s ago
        // Skips left fields if they aren't necessary, eg. 16h 0m 27s ago / 10m 7s ago
        $str = sprintf("%s%s%s%s",
                $day != 0 ? $day."d " : "",
                ($day != 0 || $hrs != 0) ? $hrs."h " : "",
                ($day != 0 || $hrs != 0 || $min != 0) ? $min."m " : "",
                $sec."s ago"
        );

        return $str;
}

将一个unix时间戳传递给它(你过去的时间),它会说多久以前它。如果您希望返回值略有不同,或者想要在其中添加数月或数年,则可以轻松修改。

答案 1 :(得分:3)

不要将$deff除以60.该值已经以秒为单位表示,您无需将其转换为秒。

那段代码有点混乱。考虑使用DateTime包;它具有内置的差异和格式化功能。

答案 2 :(得分:0)

我认为你的'$秒'应该等于'$ deff',不应该吗?

答案 3 :(得分:0)

DateInterval :: format()提供了更好的方法。

http://php.net/manual/en/dateinterval.format.php

答案 4 :(得分:0)

我用它......它处理过去和未来的时间。

/**
 * Get a string representing the duration difference in two timestamps
 * 
 * @param   int   $time   The timestamp to compare
 * @param   int   $timeBase   The base timestamp to compare against, defaults to time()
 * @return  string   Returns the duration summary
 */
public function getTimeSummary ($time, $timeBase = false) {
    if (!$timeBase) {
        $timeBase = time();
    }

    if ($time <= time()) {
        $dif = $timeBase - $time;

        if ($dif < 60) {
            if ($dif < 2) {
                return "1 second ago";
            }

            return $dif." seconds ago";
        }

        if ($dif < 3600) {
            if (floor($dif / 60) < 2) {
                return "A minute ago";
            }

            return floor($dif / 60)." minutes ago";
        }

        if (date("d n Y", $timeBase) == date("d n Y", $time)) {
            return "Today, ".date("g:i A", $time);
        }

        if (date("n Y", $timeBase) == date("n Y", $time) && date("d", $timeBase) - date("d", $time) == 1) {
            return "Yesterday, ".date("g:i A", $time);
        }

        if (date("Y", $time) == date("Y", time())) {
            return date("F, jS g:i A", $time);
        }
    } else {
        $dif = $time - $timeBase;

        if ($dif < 60) {
            if ($dif < 2) {
                return "1 second";
            }

            return $dif." seconds";
        }

        if ($dif < 3600) {
            if (floor($dif / 60) < 2) {
                return "Less than a minute";
            }

            return floor($dif / 60)." minutes";
        }

        if (date("d n Y", ($timeBase + 86400)) == date("d n Y", ($time))) {
            return "Tomorrow, at ".date("g:i A", $time);
        }
    }

    return date("F, jS g:i A Y", $time);
}