Jump to content

[SOLVED] Time Conversion, Math


D.seL

Recommended Posts

Hello everyone, new to the forum here. I have exhausted all my options on getting my little idea to work. So I am hoping maybe someone here can help.

 

First off, assume I know very little about PHP :)

 

This is the idea:

Have a Form where a user can input an elapsed time for an x amount of  races (video game)

      example : 2'34.765 or : 2:34.765

      Input is in minutes, seconds, milliseconds.

 

Have the info from the Form sent to a PHP script that would add up all the elapsed times, 'echo' the total time, and then calculate the average elapsed time for the x amount of races, and 'echo' that as well.

 

I am hitting a brick wall in all this time conversion...ie., getting PHP to add all the minutes, seconds, and milliseconds....I think maybe it is a formatting issue as the Form passes the info to the script as example : 2'34.765 or : 2:34.765.

 

I did manage to get a script working, but the user would have to input their elapsed time in seconds.milliseconds, not minutes:seconds.milliseconds. Therefore the user would have to convert their times before even submitting them. NOT GOOD LOL!!

 

My script added up the total seconds, and converted them to minutes:

    example : 105.xxx seconds would output 1.75xxxx minutes

    My desired output would be 1:45.xxx

 

Is this even possible? Does anyone understand what I am trying to do?? LOL

Any help would be GREATLY appreciated.

 

The following is the code I used to convert the seconds.milliseconds to minutes.fraction ie., 2:45.xxx outputs as 2.7500000

 

<?php
   $race1 = ($_REQUEST['race1']);
   $race2 = ($_REQUEST['race2']);
   $race3 = ($_REQUEST['race3']);
   $race4 = ($_REQUEST['race4']);
   $race5 = ($_REQUEST['race5']);
   $totalTime = ($race1 + $race2 + $race3 + $race4 + $race5);
   $timeAvg = $totalTime / 5 /60;
   echo ' Total Time (in seconds) : ', $totalTime;
   echo '<br> Time Average : ', round ( $timeAvg, 7 );
?>

Link to comment
https://forums.phpfreaks.com/topic/163866-solved-time-conversion-math/
Share on other sites

look at the strtok() function to split the components of each time to minutes and seconds based on a split around either : or '

Then multiply the minutes by 60 and add the seconds to give you a total number of seconds.

 

Do your total and average calculations based on the total seconds values you've just calculated, not on the formatted strings that you're doing in your existing code.

 

Reformat the final result to minutes and seconds before displaying. (Use the % operator)

look at the strtok() function to split the components of each time to minutes and seconds based on a split around either : or '

 

I went and had a look at that, and I must say I did not understand a bit of it.

 

I know very little about PHP, so it is hard for me to understand all the lingo used on that link. I somewhat understand your calculation description, but beyond that.....

 

Thanks for your reply, any other ideas?? or any laymans terms I can understand LOL

$races = array();
$races[] = $_REQUEST['race1'];
$races[] = $_REQUEST['race2'];
$races[] = $_REQUEST['race3'];
$races[] = $_REQUEST['race4'];
$races[] = $_REQUEST['race5'];
$totalTime = $raceCount = 0;
foreach($races as $race) {
   $minutes = strtok($race, "':");
   $seconds = strtok("':");
   $raceSeconds = $minutes * 60 + $seconds;

   $totalTime += $raceSeconds;
   $raceCount++;
}
$timeAvg = $totalTime / $raceCount;
echo 'Total Time (in seconds) : ', $totalTime;
echo '<br />Time Average (in seconds) : ', round ( $timeAvg, 7 );

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.