0

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.

MobEn
  • 65
  • 9
  • Yes, updated my post with the script which I want to include the total time. – MobEn Oct 08 '14 at 19:23
  • this may help you : http://stackoverflow.com/questions/2059603/php-function-to-convert-unix-timestamp-into-minutes-or-hours-or-days-like-digg – Diljit PR Oct 08 '14 at 19:25
  • It's just math. timestamps = seconds. start dividing. e.g. `61 -> 1 minute 1 second` – Marc B Oct 08 '14 at 19:27
  • Thanks, but I am unable to successfully implement similiar functions to my script above. That's my problem actually – MobEn Oct 08 '14 at 19:27

1 Answers1

1

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');
}
Community
  • 1
  • 1
Yoric
  • 1,761
  • 2
  • 13
  • 15
  • Thanks however I included my main post with my script where I want to include that. I am aware how to display the number but not sure how I can actually convert it to human reading in my function above. Will greatly appreciate if you could help me on that. Thanks – MobEn Oct 08 '14 at 19:24
  • Thanks, awesome. I've edited it a bit to include my own format and etc. It's greatly appreciated and answer is 100% accepted +1 up voted. Thanks again! – MobEn Oct 08 '14 at 19:55
  • ok great you made it with desired output. i edited with days/minutes/seconds: format('%a/%i/%s') – Yoric Oct 08 '14 at 19:56