将时间戳转换为秒的问题

时间:2013-07-18 02:57:22

标签: php mysql timestamp

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

1 个答案:

答案 0 :(得分: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";
}