D.seL Posted June 27, 2009 Share Posted June 27, 2009 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 More sharing options...
Mark Baker Posted June 27, 2009 Share Posted June 27, 2009 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) Link to comment https://forums.phpfreaks.com/topic/163866-solved-time-conversion-math/#findComment-864596 Share on other sites More sharing options...
D.seL Posted June 27, 2009 Author Share Posted June 27, 2009 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 Link to comment https://forums.phpfreaks.com/topic/163866-solved-time-conversion-math/#findComment-864600 Share on other sites More sharing options...
Mark Baker Posted June 27, 2009 Share Posted June 27, 2009 $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 ); Link to comment https://forums.phpfreaks.com/topic/163866-solved-time-conversion-math/#findComment-864654 Share on other sites More sharing options...
D.seL Posted June 29, 2009 Author Share Posted June 29, 2009 Thanks Mark, that did the trick for the input....I really appreciate the help! ---------------- Now playing: 4Him - The Love of God via FoxyTunes Link to comment https://forums.phpfreaks.com/topic/163866-solved-time-conversion-math/#findComment-865364 Share on other sites More sharing options...
D.seL Posted July 2, 2009 Author Share Posted July 2, 2009 I got it up and running, Thanks for the help Link to comment https://forums.phpfreaks.com/topic/163866-solved-time-conversion-math/#findComment-867603 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.