#### Do you have a question? Post it now! No Registration Necessary. Now with pictures!

**posted on**

- Greg Scharlemann

August 29, 2009, 8:40 pm

I'm trying to calculate how fast I ran based on my finish time

(00:23:59) and the total distance (3.107 miles). I've converted the

time to seconds using the following method (i use this method for

other calculations so I divide the result by 60 later to get minutes):

function hmstotime($hms)

{

list($hours, $minutes, $seconds) = explode(":",$hms);

return $hours

*** 60 ***60 + $minutes * 60 + $seconds;

}

Here's the code:

$run

___speed = (hmstotime($row['RUN'])/60) / $race___info

['RUN_DISTANCE']);

// the values of $row['RUN'] = 00:23:59

// the value of $race

___info['RUN___DISTANCE'] = 3.107

the problem is $run_speed returns: 7.71912884884

How do I convert this result into minutes, resulting in: 7:43?

If I get a $run_speed of 8.04, that should equal: 8:02

Thanks in advance.

gS

## Re: calculating miles a minute

wrote:

Well, I came up with this... but I'm sure it's not the cleanest thing

in the world. I'm open to suggestions for improvement if you have

them. Thanks

$run

___speed = explode('.', (hmstotime($row['RUN'])/60) / $race___info

['RUN_DISTANCE']);

$min

___per___mile[0] = trim($run_speed[0]); //this is the minutes

$min

___per___mile[1] = (".".trim($run_speed[1]))*60; //this is the

seconds, but i have to add a "." back because it is stripped

$seconds

___per___mile = explode('.', $min

___per___mile[1]); // multiplying

the seconds by 60 usually returns decimals that i don't care about

if($seconds

___per___mile[0] < 10) $seconds

___per___mile[0] = "0".

$seconds

___per___mile[0]; // add a 0 in front of anything less than 10

$min

___per___mile[1] = $seconds

___per___mile[0];

$avg

___run___speed = implode(":",$min

___per___mile); // put it all back

together.

## Re: calculating miles a minute

Greg Scharlemann wrote:

Is this coming from a database (looks like it)? If so, the data should

be in a TIME column type, and you can use the database functions to get

the time back in any format you wish. You can even use the database

functions to do calculations.

Look at printf() or sprintf().

--

==================

Remove the "x" from my email address

Jerry Stuckle

JDS Computer Training Corp.

jstucklex@attglobal.net

==================

## Re: calculating miles a minute

On Sat, 29 Aug 2009 14:09:08 -0700 (PDT), Greg Scharlemann wrote:

Look in the math function instead. Some combinations of floor() and

round() and judicious subtraction and multiplication will get you nice

integer seconds. sprintf() lets you format your results nicely.

--

_ o

|/)

## Re: calculating miles a minute

.oO(Greg Scharlemann)

Use Unix timestamps and PHP's date and time functions. Such timestamps

represent the number of seconds since 1970-01-01 00:00:00. You can

simply interpret your running time as the number of seconds on that day.

Your hmstotime() function returns 1439 for the given time. If you devide

that by the run distance and feed the result to gmstrftime() with some

appropriate format string, you'll get what you want. In short:

print gmstrftime('%H:%M:%S', hmstotime('00:23:59')/3.107);

returns '00:07:43'.

Just as an example what you can do with such timestamps, you could also

write your hmstotime() function like this:

function hmstotime($hms) {

return strtotime("1970-01-01 $hms GMT");

}

But in this particular case it's quite likely that your version is

faster.

Micha

## Re: calculating miles a minute

A speed is a distance divided by a time (with the result in, say,

miles/minute). You are calculating a time divided by a distance

(with the result in, say, minutes/mile). Why do you refer to the

result as a speed?

A value in minutes and seconds isn't a speed, it's a time.

#### Site Timeline

- » Excel to Html
- — Next thread in » PHP Scripting Forum

- » Is this for real?
- — Previous thread in » PHP Scripting Forum

- » URL redirection
- — Newest thread in » PHP Scripting Forum

- » Anyone Using ESET NOD32??
- — The site's Newest Thread. Posted in » Anti-Virus Software