View Full Version : Hours & minutes

01-31-2007, 12:09 PM
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?

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



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

01-31-2007, 10:20 PM
You'd be better off writing your own code. This way you'll learn more from it.

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.

01-31-2007, 11:41 PM
I agree about learning on your own ...

But on the other hand, I've learned most by
looking at other scripts and modifying things,
as well as experimenting.

I guess each person has their own learning techniques.