Quick tool to convert time into decimal
I needed a quick tool that can convert say 20 minutes into decimal, as im doing this a lot these days. Here is what i come up with, well you can google it as well and that works, but this form is a bit easier.
Just create a quick tool that takes time, e.g: 30 minutes and converts it into 0.5
And do a demo of the code required. Here are a few options that i found.
Basic Conversion Calculations
Minutes = (Hours x 60) + (Minutes) + (Seconds / 60)
Minutes = (11 x 60) + (11) + (11 / 60)
Minutes = (660) + (11) + (0.18333333)
Minutes = 671.18333333.
Complex Conversion with hours mins and secons
<?php
$hms = "2:12:0";
$decimalHours = decimalHours($hms);
function decimalHours($time)
{
$hms = explode(":", $time);
return ($hms[0] + ($hms[1]/60) + ($hms[2]/3600));
}
echo $decimalHours;
?>
Nice simple one with hours and minutes
function convert($hours, $minutes) {
return $hours + round($minutes / 60, 2);
}
I went with this one, as its nice and basic for the demo.
PHP
$template_result = "";
$get_form = "
<form method='post'>
<div class='form-group'>
<label for='numberHours'>Hours</label>
<input type='number' class='form-control' id='numberHours' aria-describedby='emailHelp' placeholder='Enter Hours' name='hours' value='0' />
<small id='numberHoursHelp' class='form-text text-muted'>Enter the number of hours to convert to decimal, default is 0.</small>
</div>
<div class='form-group'>
<label for='numberMinutes'>Minutes</label>
<input type='number' class='form-control' id='numberMinutes' aria-describedby='numberMinutesHelp' placeholder='Enter Minutes' name='minutes' />
<small id='numberMinutesHelp' class='form-text text-muted'>Enter the number of hours to convert to decimal</small>
</div>
<button type='submit' class='btn btn-primary'>Convert</button>
</form>
";
$template_result .= $get_form;
if(isset($_POST['minutes'])) {
$minutes = $_POST['minutes'];
} else {
$minutes = "";
}
if(isset($_POST['hours'])) {
$hours = $_POST['hours'];
} else {
$hours = "";
}
function convert($hours, $minutes) {
return $hours + round($minutes / 60, 2);
}
if(!is_numeric($minutes) || !is_numeric($hours) ) {
$template_result .= "<p class='message'>Missing a valid time value </p>";
return;
}
$decimal_time_val = convert($hours, $minutes);
$template_result .= "
<h1>Demo</h1>
<div class='card'>
<div class='card-header'>
Result
</div>
<div class='card-body'>
<h5 class='card-title'>$hours Hours and $minutes Minutes as Decimal is</h5>
<p class='card-text'><textarea>$decimal_time_val</textarea></p>
</div>
</div>
";