Jump to content

Convert Hours and Minutes to Decimal then submit to DB


eits

Recommended Posts

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

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;

}

?>

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!

<?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
?>

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.