473,387 Members | 1,497 Online

# seconds converted to HH-MM-SS

Hello,

I am having a nightmare of a time getting seconds converted to
hour-minute-seconds

I have a third party application that outputs only in seconds since

so if someone has been logged in for 1 minute, the time value would be 60,
if they were on for 5 minutes and 3 seconds the value would be 303

what I need is a way to take the 303 seconds and convert it to a human
0 hours 5 minutes and 3 seconds

and if possible; if hours = 0 then it wouldn't display - the same for
minutes.

-L"k"
*noob*
Jul 17 '05 #1
6 46049
"Kription" <kr******@cox.net> wrote in message
Hello,

I am having a nightmare of a time getting seconds converted to
hour-minute-seconds

I have a third party application that outputs only in seconds since

so if someone has been logged in for 1 minute, the time value would be 60,
if they were on for 5 minutes and 3 seconds the value would be 303

what I need is a way to take the 303 seconds and convert it to a human
0 hours 5 minutes and 3 seconds

and if possible; if hours = 0 then it wouldn't display - the same for
minutes.

-L"k"
*noob*

try this:

\$t = (5 * 60) + 3; // 5 minutes and 3 seconds
print date("H:i:s",-57600 + \$t);

will print "00:05:03"

--
http://www.gzentools.com -- free online php tools
Jul 17 '05 #2
Kription wrote:
I am having a nightmare of a time getting seconds converted to
hour-minute-seconds (snip) what I need is a way to take the 303 seconds and convert it to a human
0 hours 5 minutes and 3 seconds

and if possible; if hours = 0 then it wouldn't display - the same for
minutes.

#v+
<?php
function secs2hms(\$secs) {
if (\$secs<0) return false;
\$m = (int)(\$secs / 60); \$s = \$secs % 60;
\$h = (int)(\$m / 60); \$m = \$m % 60;
return array(\$h, \$m, \$s);
}

for (\$i=0; \$i<16; ++\$i) {
echo implode(', ', secs2hms(\$i*817)), "\n";
}
?>
#v-

What this script doesn't do, I'll leave up to you :)

Happy Coding :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3
Kription wrote:

what I need is a way to take the 303 seconds and convert it to a human
0 hours 5 minutes and 3 seconds

Well, I wrote this yesterday. Does the job, but I'd be very interested
to hear what people think about efficiency and processor cost. Is a
funciton like this wildly unreasonable?

// timePassed is a function to turn a time into a more human-friendly format
// Provide it with the number of seconds that have passed, and it will
return a more suitable
// description of how much time has passed.

function timePassed(\$pastTimestamp)
{
\$currentTimestamp = time();
\$timePassed = \$currentTimestamp - \$pastTimestamp; //time passed in seconds
// Minute == 60 seconds
// Hour == 3600 seconds
// Day == 86400
// Week == 604800
\$elapsedString = "";
if(\$timePassed > 604800)
{
\$weeks = floor(\$timePassed / 604800);
\$timePassed -= \$weeks * 604800;
\$elapsedString = \$weeks." weeks, ";
}
if(\$timePassed > 86400)
{
\$days = floor(\$timePassed / 86400);
\$timePassed -= \$days * 86400;
\$elapsedString .= \$days." days, ";
}
if(\$timePassed > 3600)
{
\$hours = floor(\$timePassed / 3600);
\$timePassed -= \$hours * 3600;
\$elapsedString .= \$hours." hours, ";
}
if(\$timePassed > 60)
{
\$minutes = floor(\$timePassed / 60);
\$timePassed -= \$minutes * 60;
\$elapsedString .= \$minutes." minutes, ";
}
\$elapsedString .= \$timePassed." seconds";

return \$elapsedString;
}

If you think it's what you're looking for, feel free to use it.
--
Bob
London, UK
echo Mail fefsensmrrjyaheeoceoq\! | tr "jefroq\!" "@obe.uk"
Jul 17 '05 #4
Use gmdate() instead. The extra hours that you're getting comes from the
time zone difference between your and Greenwich, and would change depending
on where you are (I got error from your code, for instance, because the
timestamp was negative).

gmdate("H:i:s", \$t) will yield exactly what's needed.

Uzytkownik "CountScubula" <me@scantek.hotmail.com> napisal w wiadomosci
news:Cv******************@newssvr25.news.prodigy.c om...
\$t = (5 * 60) + 3; // 5 minutes and 3 seconds
print date("H:i:s",-57600 + \$t);

will print "00:05:03"

--
http://www.gzentools.com -- free online php tools

Jul 17 '05 #5
Thank you..

Doh.. Guess who feels dumb now ! - it was so simple, I should have known
better.

-L"k"

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:6J********************@comcast.com...
Use gmdate() instead. The extra hours that you're getting comes from the
time zone difference between your and Greenwich, and would change
depending
on where you are (I got error from your code, for instance, because the
timestamp was negative).

gmdate("H:i:s", \$t) will yield exactly what's needed.

Uzytkownik "CountScubula" <me@scantek.hotmail.com> napisal w wiadomosci
news:Cv******************@newssvr25.news.prodigy.c om...
\$t = (5 * 60) + 3; // 5 minutes and 3 seconds
print date("H:i:s",-57600 + \$t);

will print "00:05:03"

--
http://www.gzentools.com -- free online php tools

Jul 17 '05 #6
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:6J********************@comcast.com...
Use gmdate() instead. The extra hours that you're getting comes from the
time zone difference between your and Greenwich, and would change depending on where you are (I got error from your code, for instance, because the
timestamp was negative).

gmdate("H:i:s", \$t) will yield exactly what's needed.

dont you just hate it when it right under your knose?

--