View Full Version : Converting time into seconds

08-06-2008, 03:47 PM
I had help with making time into seconds, minutes, hours, days. But how does only seconds work? I want to have it where your online within the last 150 seconds (2 1/2 minutes) and Idle for 5 minutes. This coding isn't working for me.

$checkquery = doquery('SELECT * FROM {{table}} WHERE UNIX_TIMESTAMP(`onlinetime`) >= ' . (time() - 600) . '', 'accounts');
while ($checkrow = mysql_fetch_array($checkquery))

$activetime = strtotime($checkrow['onlinetime']);
$timenow = time();
$lastonline = $timenow - $activetime;

if ($lastonline <= 150)
$online = "<font color=green><b>Online</b></font>";
elseif ($lastonline <= 300)
$online = "<font color=blue><b>Idle</b></font>";
$online = "<font color=red><b>Not Online</b></font>";

08-06-2008, 04:01 PM
That's funny, I made one just this morning:

function sec2time($sec) {
$time = '';
$seconds = $sec&#37;60;
$minutes = $sec / 60 % 60;
$hours = $sec / 3600 % 24;
$days = intval($sec / 86400) ;
$time = "$days day(s) $hours:$minutes:$seconds";
return $time;

In your script, is $activetime getting set right? Is $checkrow['onlinetime'] coming in right? You've probably already done it, but have you tried printing them each out to make sure that your calculating things with the correct values?

08-06-2008, 05:31 PM
Here is what I get when I echo the results.

Online Set: 2008-08-06 09:30:29
Active Time: 1218036629

08-07-2008, 04:34 PM

08-07-2008, 04:40 PM
Odd - which of the conditionals gets executed? The if, elseif, or the else? Is it always the same one? Also, make sure that you aren't using localized times - ie make sure they are all the same timezone. I've had this issue before when subtracting GM time from an Eastern Standard Time (where I live) and visa-versa. Check them here: http://www.csgnetwork.com/unixds2timecalc.html If time() and the time you get from the database are localized differently...

08-07-2008, 05:55 PM
It should be the same time zone, because all the other online functions on the site work. Just this one isn't working. All of them always say the same thing. If I do it normal, it all says Online, when I am the only one online, if I alter the number, it all says Idle. if I comment out the query, it all says Not Online. But I can't get each to work.