eits Posted September 4, 2007 Share Posted September 4, 2007 Hi! I need to convert data from two text boxes (txthours and txtmins) and convert it into decimal format. I have this code: <?php $addtoworked = $_POST['hoursworked'] + $_POST['minsworked']/60; ?> to convert data into decimal. I Also have: <?php $first_number = $row_Recordset1['hoursallocated']; $second_number = $row_Recordset1['hoursworked']; $sum_total = $first_number - $second_number; $timepart = explode(".", $sum_total); $hoursleft = $timepart[0]; $minutesleft = (intval($timepart[1]) / 10) * 60; ?> However I now need to add $addtoworked from $hoursleft and then add this to the $hoursworked to tell me how many hours the client has left. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/67948-convert-hours-and-minutes-to-decimal-then-submit-to-db/ Share on other sites More sharing options...
Psycho Posted September 4, 2007 Share Posted September 4, 2007 Not to be rude, but your code is confusing. Variables names like $first_number and $second_number make the process hard to follow. I fond it odd that you have a variable named "sum_total" wich is actually a difference between two numbers. Plus, I don't know which variables are in decimal format and which ones are not. And, the calculation you have to determine $minutesleft makes no sense to me. If timepart[1] = .25, then you should come up with 15 minutes. But, I think that formula would result in 1.5 I suggest just leaving everything in decimal format and using a function to change the display to hour/minute format. Here are a couple functions to convert from HH:MM to decimal and back. Hope they help. <?php function hourstodecimal ($timeinhours) { $timeparts = explode(':', $timeinhours); return $timeparts[0] + ($timeparts/60); } function decimaltohours ($timeindecimal) { $hours = floor($timeindecimal); $minutes = str_pad( (($timeindecimal - $hours)*60), 2, '0'); return $hours.':'.$minutes; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67948-convert-hours-and-minutes-to-decimal-then-submit-to-db/#findComment-341578 Share on other sites More sharing options...
eits Posted September 4, 2007 Author Share Posted September 4, 2007 Not to be rude, but your code is confusing. Variables names like $first_number and $second_number make the process hard to follow. I fond it odd that you have a variable named "sum_total" wich is actually a difference between two numbers. Plus, I don't know which variables are in decimal format and which ones are not. And, the calculation you have to determine $minutesleft makes no sense to me. If timepart[1] = .25, then you should come up with 15 minutes. But, I think that formula would result in 1.5 I suggest just leaving everything in decimal format and using a function to change the display to hour/minute format. Here are a couple functions to convert from HH:MM to decimal and back. Hope they help. <?php function hourstodecimal ($timeinhours) { $timeparts = explode(':', $timeinhours); return $timeparts[0] + ($timeparts/60); } function decimaltohours ($timeindecimal) { $hours = floor($timeindecimal); $minutes = str_pad( (($timeindecimal - $hours)*60), 2, '0'); return $hours.':'.$minutes; } ?> Thats fine - I am very new to PHP! So how would I call a function to convert say $thistimein into a decimal? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/67948-convert-hours-and-minutes-to-decimal-then-submit-to-db/#findComment-341581 Share on other sites More sharing options...
eits Posted September 4, 2007 Author Share Posted September 4, 2007 Could you please give me an example?? I am trying: <?php echo $timeindecimal['1.55'; ?> but it doesnt work Quote Link to comment https://forums.phpfreaks.com/topic/67948-convert-hours-and-minutes-to-decimal-then-submit-to-db/#findComment-341590 Share on other sites More sharing options...
Psycho Posted September 5, 2007 Share Posted September 5, 2007 <?php function hourstodecimal ($timeinhours) { $timeparts = explode(':', $timeinhours); return ($timeparts[0] + ($timeparts[1]/60)); } function decimaltohours ($timeindecimal) { $hours = floor($timeindecimal); $minutes = str_pad( (($timeindecimal - $hours)*60), 2, '0'); return $hours.':'.$minutes; } $time_in_hrs = "5:15"; echo $time_in_hrs . ' in decimal format is ' . hourstodecimal($time_in_hrs); //Output: 5:15 in decimal format is 5.25 echo "<br><br>"; $time_in_dec = "3.75"; echo $time_in_dec . ' in hours:min format is ' . decimaltohours($time_in_dec); //Output: 3.75 in hours:min format is 3:45 ?> Quote Link to comment https://forums.phpfreaks.com/topic/67948-convert-hours-and-minutes-to-decimal-then-submit-to-db/#findComment-341754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.