# calculating miles a minute

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

•  Subject
• Author
• Posted on

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

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.

## Re: calculating miles a minute

.oO(Gordon Burditt)

Call it "pace", measured in min/km or min/mile.

Micha

## Re: calculating miles a minute

Michael Fesser wrote:

Good call. "Four Minute Mile" comes to mind.

--
Joe Wright
"It's often easier to ask forgiveness than to get permission."

\$run_pace it is.

## Re: calculating miles a minute

[...]

Same way you converted it just now.