josborne Posted June 3, 2009 Share Posted June 3, 2009 I have am trying to build a very quick and dirty calculator that simply adds and subtracts one time value from another. Most of the code is cobbled together from other places as I am really do not work in PHP much at all. The issue I am having is that I need to be able to calculate times down to milliseconds (00:45:01.123). However, the code I have come up with truncates everything after the decimal. Since I only barely undestand the code, I have been unable to detemine why this is happening. Any help would be greatly appreciate. if ( $op == "add" ) { // Calculate total time // 0=HH, 1=MM, 2=SS $number1 = explode(":", $number1); $number2 = explode(":", $number2); // Convert each time to seconds $number1_seconds = (($number1[0] * 3600) + ($number1[1] * 60) + ($number1[2])); $number2_seconds = (($number2[0] * 3600) + ($number2[1] * 60) + ($number2[2])); // Add to produce total $total = $number1_seconds + $number2_seconds; // Verifying that decimals still exist - it does at this point in the code echo $total ; echo '<br /><br />'; // Convert total from seconds to full time again $_total_time["hrs"] = floor(($total / 3600)); $_total_time["mins"] = floor(($total - ($_total_time["hrs"] * 3600)) / 60); $_total_time["secs"] = floor(($total - ($_total_time["hrs"] * 3600) - ($_total_time["mins"] * 60))); // Prefix with 0s where necessary if (strlen($_total_time["hrs"]) == 1) $_total_time["hrs"] = "0" . $_total_time["hrs"]; if (strlen($_total_time["mins"]) == 1) $_total_time["mins"] = "0" . $_total_time["mins"]; if (strlen($_total_time["secs"]) == 1) $_total_time["secs"] = "0" . $_total_time["secs"]; // Format total time $total_time = $_total_time["hrs"] . ":" . $_total_time["mins"] . ":" . $_total_time["secs"]; echo $total_time; } if ( $op == "sub" ) { // Calculate total time // 0=HH, 1=MM, 2=SS $number1 = explode(":", $number1); $number2 = explode(":", $number2); // Convert each time to seconds $number1_seconds = (($number1[0] * 3600) + ($number1[1] * 60) + ($number1[2])); $number2_seconds = (($number2[0] * 3600) + ($number2[1] * 60) + ($number2[2])); // Add to produce total $total = $number1_seconds - $number2_seconds; // Verifying that decimals still exist echo $total ; echo '<br /><br />'; // Convert total from seconds to full time again $_total_time["hrs"] = floor(($total / 3600)); $_total_time["mins"] = floor(($total - ($_total_time["hrs"] * 3600)) / 60); $_total_time["secs"] = floor(($total - ($_total_time["hrs"] * 3600) - ($_total_time["mins"] * 60))); // Prefix with 0s where necessary if (strlen($_total_time["hrs"]) == 1) $_total_time["hrs"] = "0" . $_total_time["hrs"]; if (strlen($_total_time["mins"]) == 1) $_total_time["mins"] = "0" . $_total_time["mins"]; if (strlen($_total_time["secs"]) == 1) $_total_time["secs"] = "0" . $_total_time["secs"]; // Format total time $total_time = $_total_time["hrs"] . ":" . $_total_time["mins"] . ":" . $_total_time["secs"]; echo $total_time; } ?> Link to comment https://forums.phpfreaks.com/topic/160839-solved-simple-question-regarding-time-calculation/ Share on other sites More sharing options...
josborne Posted June 3, 2009 Author Share Posted June 3, 2009 I can't seem to find the "modify" button so I am just replying. to give a little more detail: I need to be able to add and subtract numbers such like this: 00:44:12.581 + 00:02:32.314 Link to comment https://forums.phpfreaks.com/topic/160839-solved-simple-question-regarding-time-calculation/#findComment-848847 Share on other sites More sharing options...
josborne Posted June 4, 2009 Author Share Posted June 4, 2009 Is there really no option to edit a post here? Weird. To give a little more detail: in the example above - 00:44:12.581 + 00:02:32.314 The result I should get is 00:46:44. 895 But what I am getting is 00:46:44 Any help would really be appreciated. Link to comment https://forums.phpfreaks.com/topic/160839-solved-simple-question-regarding-time-calculation/#findComment-849250 Share on other sites More sharing options...
PFMaBiSmAd Posted June 4, 2009 Share Posted June 4, 2009 floor() rounds down to the nearest whole integer, thereby removing any decimal/fractional time. Link to comment https://forums.phpfreaks.com/topic/160839-solved-simple-question-regarding-time-calculation/#findComment-849258 Share on other sites More sharing options...
josborne Posted June 4, 2009 Author Share Posted June 4, 2009 That did it. Pretty obvious now Thanks a lot! Link to comment https://forums.phpfreaks.com/topic/160839-solved-simple-question-regarding-time-calculation/#findComment-849337 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.