I have a small issue while calculating sum of total hours
My working hours is like:
Day1 - 12:23 Hours Day2 - 11:43 Hours Day3 - 10:18 HoursHow can I get it by php? Thanks in advance
Noich
13.7k15 gold badges61 silver badges90 bronze badges
asked Mar 13, 2013 at 12:02
0
Just add them like this:
$day1hours = "12:23"; $day2hours = "11:43"; $day1 = explode(":", $day1hours); $day2 = explode(":", $day2hours); $totalmins = 0; $totalmins += $day1[0] * 60; $totalmins += $day1[1]; $totalmins += $day2[0] * 60; $totalmins += $day2[1]; $hours = $totalmins / 60; $minutes = $totalmins % 60; $totalhours = "$hours.$minutes";It is quite elaborate like this, but it just to show the idea: calculate back to minutes, add minutes together, then calculate back to hours.
Note that PHP has quite extensive support for dates and times. Consider using that: //www.php.net/manual/en/book.datetime.php
admdrew
3,7404 gold badges26 silver badges39 bronze badges
answered Mar 13, 2013 at 12:06
Bart FriederichsBart Friederichs
32.3k15 gold badges96 silver badges187 bronze badges
This technique will give exact total hours and minutes. The above answer gives me 1.25.15 when i add 00:30 and 00:45
<?php $hourMin = array("00:30","00:45"); //an Multiple Hour:Minutes array $hours = 0; $mins = 0; foreach($hourMin as $val) { $explodeHoursMins = explode(':',$val); $hours += $explodeHoursMins[0]; $mins += $explodeHoursMins[1]; } $minToHours = date('H:i', mktime(0,$mins)); //Calculate Hours From Minutes $explodeMinToHours = explode(':',$minToHours); $hours += $explodeMinToHours[0]; $finalMinutes = $explodeMinToHours[1]; echo $hours." : ".$finalMinutes; ?>answered Feb 2, 2018 at 11:46
Vishnu BhadoriyaVishnu Bhadoriya
1,6161 gold badge21 silver badges28 bronze badges
If you use this function to calculate time calculation then it will work
function sumTime($initialHour, $finalHour) { $h = date('H', strtotime($finalHour)); $m = date('i', strtotime($finalHour)); $s = date('s', strtotime($finalHour)); $tmp = $h." hour ".$m." min ".$s." second"; $sumHour = $initialHour." + ".$tmp; $newTime = date('H:i:s', strtotime($sumHour)); return $newTime; };For more explanation refer this link: Time Calculation in php
answered Mar 13, 2013 at 12:16
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given an array containing the time in hr:min:sec format. The task is to calculate the total time. If the total time is greater then 24 hours then
the total time will not start with 0. It will display the total time. There are two ways to calculate the total time from the array.
- Using strtotime() function
- Using explode() function
Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time
in h:m:s format.
Syntax
Example 1: This example reads the values from the array and converts it into the time format.
php
<?php
$time = [
'00:04:35', '00:02:06', '01:09:12',
'09:19:04', '00:17:49', '02:13:59',
'10:10:54'
];
$sum = strtotime('00:00:00');
$totaltime = 0;
foreach( $time as $element ) {
$timeinsec = strtotime($element) - $sum;
$totaltime = $totaltime + $timeinsec;
}
$h = intval($totaltime / 3600);
$totaltime = $totaltime - ($h * 3600);
$m = intval($totaltime / 60);
$s = $totaltime - ($m * 60);
echo ("$h:$m:$s");
?>
Using explode() function: The explode() function is used to break a string into an array.
Syntax
Example 2: This example reads the values from an array and converts it into the time
format.
php
<?php
$arr = [
'00:04:35', '00:02:06', '01:09:12',
'09:19:04', '00:17:49', '02:03:59',
'10:10:54'
];
$total = 0;
foreach( $arr as $element):
$temp = explode(":", $element);
$total+= (int) $temp[0] * 3600;
$total+= (int) $temp[1] * 60;
$total+= (int) $temp[2];
endforeach;
$formatted = sprintf('%02d:%02d:%02d',
($total / 3600),
($total / 60 % 60),
$total % 60);
echo $formatted;
?>