1

I'm trying to convert Time format 00:00:00 to decimal 0.00.

I tried this:

<?php 
   $hourswork_wk1 = $deposit26['hours_worked'];
   $decimalHourswk1 = decimalHours($hourswork_wk1);
   decimalHours();

    function decimalHours($timewk1)
    {
        $hourswork_wk1 = explode(":", $timewk1);
        return ($hourswork_wk1[0] + ($hourswork_wk1[1]/60) + ($hourswork_wk1[2]/3600));
        echo $decimalHours
    }


;?>

EDIT

<?php 
   $hourswork_wk1 = $deposit26['hours_worked'];
   $decimalHourswk1 = decimalHours($hourswork_wk1);

    function decimalHours($timewk1)
    {
        $hourswork_wk1 = explode(":", $timewk1);
        return ($hourswork_wk1[0] + ($hourswork_wk1[1]/60) + ($hourswork_wk1[2]/3600));

    }
?>

I get a call to undefined function decimalHours() error. What am I doing wrong?

Sebastian Farham
  • 815
  • 2
  • 13
  • 27

1 Answers1

0

Alright for some reason the code I have written never worked for me. So after looking around for something more tangible I stumbled upon Philip Norton code which works like a charm with dynamic variables. Suit yourself.

script

<?php
//$week1hours = $deposit26['hours_worked'];
$week1hours = "14:33:38";
function time_to_decimal($time) {
    $timeArr = explode(':', $time);
    $decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
    return $decTime;
}
echo time_to_decimal($week1hours);
?>

result

14.560555555556

bonus : limit to 2 decimals

Use round to only get 2 decimals

<?php
//$week1hours = $deposit26['hours_worked'];
$week1hours = "14:33:38";
function time_to_decimal($time) {
    $timeArr = explode(':', $time);
    $decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
    return $decTime;
}
$totalhours= time_to_decimal($week1hours);
echo round($totalhours, 2);
?>

result

14.56
Barmar
  • 741,623
  • 53
  • 500
  • 612
Sebastian Farham
  • 815
  • 2
  • 13
  • 27