How do I add multiple hours in php to have the total hours? -
i'm working on time in , time out system, editing codes. format of time being recorded in h:i:s. system allows user see time in office in table. in table, first row: have date-in, time-in, date-out, time-out , number of hours employee stayed in office. example, time 09:00:00 18:00:00 hours shown 9.00 hours because made in decimal format. if 09:20:00 18:00:00 hours of should in decimal also. example: 8.70 hours. that.
now, table being shown whole month. below table, there total hours in format hours , mins (ex. 77 hours , 6 mins) , in decimal (77.10 hours). how compute total hours?
the code below computation of hours. getting hours of time-in , out.
$n = $totalmins/60; $whole = floor($n); // 1 $fraction = $n - $whole; $totalhrs += $whole; $totalmins = round($fraction*60); $totalmins = sprintf("%02d", $totalmins); $elapseddeci = round($fraction, 2); $elapseddeci += $totalhrs; $elapseddeci = number_format($elapseddeci, 2); $elapsed = "$elapseddeci";
the main problem approach was, mixed calculation , formatting of time, made difficult contiue calculations results.
i advise strictly seperate calculation , displayment. way, can calculate times first, , display them in formatted manner.
by reading code, assume have working time of each day in form of amount minutes @ hand, example in array (for sake of explanation , readability, let's pretend our month 7 days):
$minutes_stayed = array(480, 500, 460, 503, 429, 0, 0);
it should easy you, create similar structure kind of database querying doing.
now, should first try , format minutes timespans want work with, hours or decimal hours. store single timespan $minutes = 480;
, perform mathematical wizardry. note, did not string formatting yet:
$formatted = array(); $formatted["hours"] = floor($minutes/60); $formatted["minutes"] = $minutes - $formatted["hours"]*60; $formatted["decimal"] = $formatted["hours"] + $formatted["minutes"]/60;
after did so, free whatever want. sum/ subtract/ print/ throw away/... possibilities endless.
have @ following class. deduction of existing code, strict seperation of calculation , displayal:
class timecalculator { public static function print_as_decimal( $decimal ) { echo number_format($decimal, 2, '.', '')." hours"; } public static function print_as_h_and_min( $hours, $minutes ) { echo $hours." hours , ".$minutes." minutes"; } public static function format( $minutes ) { $formatted = array(); $formatted["hours"] = floor($minutes/60); $formatted["minutes"] = $minutes - $formatted["hours"]*60; $formatted["decimal"] = $formatted["hours"] + $formatted["minutes"]/60; return $formatted; } }
it should not hard figure out, each of functions does. using class have need work time. try code:
$minutes_stayed = array(480, 500, 460, 503, 429, 0, 0); foreach($minutes_stayed $day => $time) { $formatted = timecalculator::format($time); echo timecalculator::print_as_decimal($formatted["decimal"])." equals "; echo timecalculator::print_as_h_and_min($formatted["hours"], $formatted["minutes"])."<br />"; } $formatted_total = timecalculator::format(array_sum($minutes_stayed)); echo "total: "; echo timecalculator::print_as_decimal($formatted_total["decimal"])." or "; echo timecalculator::print_as_h_and_min($formatted_total["hours"], $formatted_total["minutes"])."<br />";
as can see, right seperation of calculaton , displayal, the thing need calculate total amount of hours, array_sum($minutes_stayed)
.
Comments
Post a Comment