Re: Calculating time differences.

From: Date: Wed, 17 Oct 2001 00:40:06 +0000
Subject: Re: Calculating time differences.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-71285@lists.php.net to get a copy of this message
On Wed, 17 Oct 2001 09:07, Mike Eheler wrote: > If I have two times, in text format: > > 14:32:23 and 17:56:36 > > How can I calculate the difference between the two, and have it > returned like: > > 03:24:13 > > Then beyond that, once I have a bunch of these differences, such as: > > 03:24:13 > 00:45:52 > etc > > I need be able to add those all up as I go, so it'd be a loop and it'd > keep a running total. > > The numerical equivalent of what I want to do is: > > $total = 0; > while ($data = mysql_fetch_array($result)) { > $difference = $data['end'] - $data['start']; // for example, (end = > 5) - (start = 3) > echo $difference; // 2 > $total += $difference; > } > echo $total; > > Mike You might use explode to park the hh, mm, ss parts of the time in an array, then use the elements of the array and mktime to turn the values into a unix timestamp. You could then add the timestamp values and finally convert back to hh:mm:ss Or, if that doesn't work, just convert to seconds by multiplying the hour part by 3600, the minute by 60. -- David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA Useless Invention: Reduced calorie water.

« previous php.general (#71285) next »