0

基本上我要做的是将mysql时间戳转换为“xx秒前”格式。我使用这个函数 - $age = time() - strtotime($eventTime)。我得到了结果,但它在一小时后(-3600 秒)以秒为单位显示时间。EG 如果我现在发布,它会显示(-3600 秒前)。它可能是什么问题?

4

1 回答 1

0

假设您的 MySQL 会话和您的 PHP 设置都共享相同的时区,您可以使用类似这样的方法来获得合适的英语时间段:

function toFriendlyTime($seconds) {
    // Used in the item view, this function returns a 'friendly', rough-idea type time, like "3 hours" or "4 months"
    $measures = array(
        'month'=>30*24*60*60,
        'week'=>7*24*60*60,
        'day'=>24*60*60,
        'hour'=>60*60,
        'minute'=>60,
        'second'=>1,
    );
    foreach ($measures as $label=>$amount) {
        if ($seconds >= $amount) {
            $howMany = floor($seconds / $amount);
            return $howMany." ".$label.($howMany > 1 ? "s" : "");
        }
    }
    return "1 second";
}
于 2013-07-18T03:44:56.740 回答