php - Converting MysQl Time to decimal format -


i'm trying convert time format 00:00:00 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 call undefined function decimalhours() error. doing wrong?

alright reason code have written never worked me. after looking around more tangible stumbled upon philip norton code works charm 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 2 decimals

use round 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 

Comments

Popular posts from this blog

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -