Jump to content

Convert Hours and Minutes to Decimal then submit to DB


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.