MobEn
MobEn

Reputation: 65

How to calculate unix timestamp in days/minutes/seconds?

I've got script to compare login date/time and logout date/time after that the difference between these two values is added to totaltime column in MSSQL 2005 table. For example: If the user is online just a few seconds, it will add a value of around 98 unix timestamp for example. So how to display that value onto the website as human reading: days/minutes/seconds if possible?

Thanks a lot for checking my question. It will be greatly appreciated to help me out.

That's my rank script where I want to include that:

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

So, what's the best way to include what I want to my rank script above so I can display total time online per user based on the totaltime column in my data table? I know how to include it but I got confused how to convert it to human reading in this function above.

Upvotes: 0

Views: 858

Answers (1)

Yoric
Yoric

Reputation: 1784

This should suit your needs :

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

and this function :

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');
}

based on this

Edit: putting it all together with your existing script : (i guess ?)

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');
}

Upvotes: 1

Related Questions