如何计算unix时间戳以天/分/秒为单位?

时间:2014-10-08 19:09:26

标签: php timestamp

我有脚本来比较登录日期/时间和注销日期/时间,之后将这两个值之间的差异添加到MSSQL 2005表中的totaltime列。例如:如果用户在线仅几秒钟,它将添加大约98 unix时间戳的值。那么如何将这个值作为人类阅读显示在网站上:如果可能的话,天/分/秒?

非常感谢您查看我的问题。非常感谢帮助我。

这是我的排名脚本,我希望将其包括在内:

function TotalTimeRank()
{
    $db = $this->database[GDB];
    $num_rows = $db->doQuery('SELECT TOP 100 ID, Name, (SELECT SUM(CoolPoint) FROM DATA WHERE DATA.User = INFO.User AND DATA.Auth IN(1, 2)) as Points FROM INFO WHERE INFO.User NOT IN (1199,16300) ORDER BY Points DESC');
    if ($num_rows == -1)
    {
        $db->getError();
        return;
    }

    $n = 1;
    $content = '';
    while ($row = $db->doRead())
    {
        $data = array('rank-id' => $row['Num'], 'rank-pos' => $n++, 'rank-name' => $row['Name'], 'rank-points' => number_format(intval($row['Points'])));
        $content .= Template::Load('pointrank-' . ($n % 2 == 1 ? 2 : 1), $data);
    }

    $this->content = Template::Load('total_pointrank', array('ranks' => $content));
}

那么,最好的方法是将我想要的内容包含在上面的排名脚本中,以便根据totaltime表格中的data列显示每位用户的在线总时间?我知道如何包含它,但我很困惑如何在上面的函数中将它转换为人类阅读。

1 个答案:

答案 0 :(得分:1)

这应该符合您的需求:

$timestamp_diff = 456; // your timestamp difference here
echo secondsToTime($timestamp_diff);

和这个功能:

function secondsToTime($seconds) { // returns days/hours/minutes/seconds
    $dtF = new DateTime("@0");
    $dtT = new DateTime("@$seconds");
    return $dtF->diff($dtT)->format('%a/%h/%i/%s');
}

基于this

编辑:将所有内容与现有脚本放在一起: (我猜?)

function TotalTimeRank()
{
    $db = $this->database[GDB];
    $num_rows = $db->doQuery('SELECT TOP 100 ID, Name, (SELECT SUM(CoolPoint) FROM DATA WHERE DATA.User = INFO.User AND DATA.Auth IN(1, 2)) as Points FROM INFO WHERE INFO.User NOT IN (1199,16300) ORDER BY Points DESC');
    if ($num_rows == -1)
    {
        $db->getError();
        return;
    }

    $n = 1;
    $content = '';
    while ($row = $db->doRead())
    {
        $data = array('rank-id' => $row['Num'], 'rank-pos' => $n++, 'rank-name' => $row['Name'], 'rank-points' => $this->secondsToTime(intval($row['Points'])));
        $content .= Template::Load('pointrank-' . ($n % 2 == 1 ? 2 : 1), $data);
    }

    $this->content = Template::Load('total_pointrank', array('ranks' => $content));
}

function secondsToTime($seconds) { // returns days/minutes/seconds
    $dtF = new DateTime("@0");
    $dtT = new DateTime("@$seconds");
    return $dtF->diff($dtT)->format('%a/%i/%s');
}