Jump to content

Calculating time based off speed and mileage


barbgd

Recommended Posts

I am trying to complete a program for calculating time based off speed and distance traveled for creating motorcyle enduro route sheets. I am posting a test page I am working on. I know the formulas are probably wrong for getting the proper times and would appreciate any input.

 

If you run this script the entries work most of the time. The problem is that at 24MPH the display "8 miles at 09:19:59" should have rounded to 9:20:00. This throws all following calculations off by 1 second.

 

Thanks in advance!!

 

 

$mph = 24; //Starting MPH. This value could change with any or each mileage entry

$start_mileage = 0;

$start_time = "09:00:00"; //Race starts at 9:00am

$today_start =  mktime (0,0,0,date("m")  ,date("d"),date("Y")); //Time at midnight. Using this to get the time difference for the first mileage entry when compared to the race start time.

 

//test mileage entries

$mileage[] = 2.80;

$mileage[] = 3.30;

$mileage[] = 3.40;

$mileage[] = 3.94;

$mileage[] = 5.35;

$mileage[] = 8.00;

$mileage[] = 8.10;

$mileage[] = 9.38;

$mileage[] = 9.60;

$mileage[] = 11.40;

$mileage[] = 11.60;

$mileage[] = 12.40;

$mileage[] = 16.36;

$mileage[] = 16.40;

$mileage[] = 21.90;

$mileage[] = 22.30;

$mileage[] = 35.00;

$mileage[] = 35.10;

$mileage[] = 37.60;

$mileage[] = 39.96;

 

 

//Calculations for time: miles / speed * 60

$loop_cnt = 0;

$previous_mile = 0;

foreach ($mileage as $value) {

 

$start = strtotime ($start_time);

 

if($loop_cnt == 0) //use this for the first mileage entry

{

$calc = ($value/$mph)*60;

$time_parts = explode(".",$calc);

$minutes =  $time_parts[0];

$seconds =  substr(($time_parts[1] * 60), 0, 2);

$previous_mile = $value; //add value for calculating differences between future entries and this one.

}

else

{

                  //Use this for any additional entries to get just the time from the last

                  //entry to the current mileage entry and add them together

 

$new_value = $value - $previous_mile;

$calc = ($new_value/$mph)*60;

$time_parts = explode(".",$calc);

$minutes =  $time_parts[0];

$seconds =  substr(($time_parts[1] * 60), 0, 2);

$previous_mile = $value;

}

 

$new_time = mktime (0,$minutes,$seconds);

$set_time = $new_time - $today_start + $start;

 

$loop_cnt++;

 

$dfmt = date("H:i:s", $set_time); //Format the time

 

    echo "$value miles at $dfmt<br> <br>\n";

 

$start_time = $dfmt; //Store the time for the next loop

}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.