PDA

View Full Version : Hours & minutes

b_hole
01-31-2007, 11:09 AM
I'm selecting from a database (among other information) a time looks like this: 1170229906.
I'm trying to find a way to print the hours & minuted differences between each couple, when I'm using while (\$rs=mysql_fetch_assoc(\$result)) {.

For example, if the first time is 1170229906 and the second is 1170240253 - the result should be 2:53.

Any ideas?

mlseim
01-31-2007, 05:00 PM
See if the script below helps out ...
(I found it on the internet and tested it out OK).

<?php

\$time1="1170240253";
\$time2="1170229906";

echo timeDiff(\$time1,\$time2);

function timeDiff(\$timestamp1,\$timestamp2){

\$difference = \$timestamp1 - \$timestamp2;

\$days = floor(\$difference/86400);
\$difference = \$difference - (\$days*86400);

\$hours = floor(\$difference/3600);
\$difference = \$difference - (\$hours*3600);

\$minutes = floor(\$difference/60);
\$difference = \$difference - (\$minutes*60);

\$seconds = \$difference;
//\$output = "\$days Days, \$hours Hours, \$minutes Minutes, \$seconds Seconds";
\$output = "\$hours:\$minutes:\$seconds";

return \$output;
}
?>

Output is: 2:52:27

aedrin
01-31-2007, 09:20 PM

Of course you can read other people's code. But you won't learn as much when you actually write it yourself.

The only concept you need to understand is that those numbers are just seconds. So if you subtract \$time2 from \$time1, you have the number of seconds between 2 times.

Divide by 60 to get minutes, divide by 3600 to get hours. Use the remainder operator as in the previous post to get the amount of hours and then the remaining seconds.

mlseim
01-31-2007, 10:41 PM