HarryMW Posted March 20, 2011 Share Posted March 20, 2011 Ok so I basically have two textfields and a submit button. The first text field basically allows the user to input a duration of an event in the format of Hours:Minutes:Seconds such as: 02:30:00 is two and a half hours. The second textfield allows the user to put in an average time 'something' does something in the same format as above. So for example 00:20:00 is 20 minutes. 1st Textfield: <input name="tmarrtextfield" type="text" class="textbox" id="tmarrtextfield" value="00:00:00"> 2nd Textfield: <input name="tmatttextfield" type="text" class="textbox" id="tmatttextfield" value="00:00:00"> What I need is for when the form is submitted to the same page.. the code to...: 1. Convert the normal text info into actual time data. 2. Divide the duration by the average timing so for example: 02:30:00 (150 minutes) / 00:20:00 (20 minutes) = 7.5 (Would return a rounded down number or up, doesn't matter) then store the number as a variable that will be echoed later. Harry. Link to comment https://forums.phpfreaks.com/topic/231198-text-field-time-calculation/ Share on other sites More sharing options...
.josh Posted March 20, 2011 Share Posted March 20, 2011 use explode() to get each increment of the time, and mktime() to create a timestamp, then date() to format it as minutes only, then divide and round() to round the number. Link to comment https://forums.phpfreaks.com/topic/231198-text-field-time-calculation/#findComment-1189998 Share on other sites More sharing options...
sasa Posted March 20, 2011 Share Posted March 20, 2011 for 2nd <?php function my_sec($a){ $a = explode(':', $a); $out = $a[0]; for($i=1;$i<count($a);$i++){ $out *= 60; $out += $a[$i]; } return $out; } $time1 = '02:30:00'; $time2 = '30:00'; echo my_sec($time1) / my_sec($time2); ?> Link to comment https://forums.phpfreaks.com/topic/231198-text-field-time-calculation/#findComment-1190001 Share on other sites More sharing options...
HarryMW Posted March 20, 2011 Author Share Posted March 20, 2011 Thank you Crayon, I did knock up my own script before reading the submition by sasa which worked. Sasa... That works brilliantly. What would I need to add to the script to finalise it slightly. As currently you just have a count of the overall amount of movements, I would like to add another count but of movements possible per hour. How can I achieve this? Link to comment https://forums.phpfreaks.com/topic/231198-text-field-time-calculation/#findComment-1190012 Share on other sites More sharing options...
sasa Posted March 20, 2011 Share Posted March 20, 2011 1 hours is 3600 seconds just 3600 divide with my_sec($time) Link to comment https://forums.phpfreaks.com/topic/231198-text-field-time-calculation/#findComment-1190041 Share on other sites More sharing options...
HarryMW Posted March 20, 2011 Author Share Posted March 20, 2011 If I try 02:30:00 and 00:30:00 I get an answer of 2.5 - Not sure how I can iron that lil problem out there as well you should get 2 movements an hour. Also when I put the: $hourresult = my_sec($time1) / 3600; Under the rest of the code, it messes up the original full count and only shows the per hour. Link to comment https://forums.phpfreaks.com/topic/231198-text-field-time-calculation/#findComment-1190049 Share on other sites More sharing options...
HarryMW Posted March 20, 2011 Author Share Posted March 20, 2011 I would edit but I cant so I will have to re-post in accordance to what you said sasa. Here is the code in full including the last bit for the movements per hour: // TIME FUNCTION FOR THE ARRIVALS function my_sec($a){ $a = explode(':', $a); $out = $a[0]; for($i=1;$i<count($a);$i++){ $out *= 60; $out += $a[$i]; } return $out; } $time1 = $_POST['tmarrtextfield']; $time2 = $_POST['tmatttextfield']; $numberresult = my_sec($time1) / my_sec($time2); $hourresult = my_sec($time1) / 3600; Problem: If for example I put in 30 minutes and click the button it will come up with an answer of 3 for moves per hour, which is impossible. Or. If I put in 20 minutes and try to get the moves per hour I get a return of 2, which is incorrect too... Harry. Link to comment https://forums.phpfreaks.com/topic/231198-text-field-time-calculation/#findComment-1190057 Share on other sites More sharing options...
sasa Posted March 21, 2011 Share Posted March 21, 2011 $hourresult = 3600 / my_sec($time2); Link to comment https://forums.phpfreaks.com/topic/231198-text-field-time-calculation/#findComment-1190155 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.