簡體   English   中英

PHP:幾分鍾前,幾秒鍾前:這是正確編碼的嗎?

[英]PHP: Minutes ago, seconds ago: is this correct coded?

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";
}

我認為分鍾前是對的,但不是秒? 我應該怎么做“秒前”吧?

我使用類似於此的小功能,可能不是最好的,不會處理未來的時間,但如果您想要的只是過去,那就沒關系。

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時間戳傳遞給它(你過去的時間),它會說多久以前它。 如果您希望返回值略有不同,或者可能希望在其中添加數月或數年,則可輕松修改。

不要將$deff除以60.該值已經以秒表示,您無需將其轉換為秒。

那段代碼有點混亂。 考慮使用DateTime包; 它具有內置的差異和格式化功能。

我認為你的'$ seconds'應該等於'$ deff',不應該嗎?

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

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

我用它...它處理過去和未來的時間。

/**
 * 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM