【问题标题】:Issue with converting timestamp to seconds将时间戳转换为秒的问题
【发布时间】:2013-07-18 02:57:22
【问题描述】:

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

【问题讨论】:

  • 不看你的数据就不能说太多,这可能是因为你生成 $eventTime 的位置与你检查的服务器有 1 小时的差距time()
  • 好吧,我使用的是本地主机。我想知道时区设置或smth在哪里?
  • 如何为$eventTime创造价值
  • 时区应该无关紧要,因为这两个函数都应该在 UTC/GMT 中给出秒数。所以你是说不管$age大于3600是什么?
  • $eventTime = $row['time'] and 'time' 是记录到表的时间。

标签: php mysql timestamp


【解决方案1】:

假设你的 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";
}

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 2016-03-04
    • 2013-03-09
    • 2017-10-24
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多