[英]Getting two hours more than required
I am getting times in format of "hh:mm:ss". 我正在以“ hh:mm:ss”的格式获取时间。 I am doing sum of all those times.
我正在做所有这些时间的总和。 But here I am getting two hours more than required.
但是在这里,我得到了比要求多两个小时的时间。 so, where is the actual problem.
因此,实际问题在哪里。 My code is like,
我的代码就像
foreach($result as $key=>$value)
{
$total_markin_time[] = date('H:i:s', strtotime($value['clock_in']));
}
Getting result like, 得到这样的结果,
00:33:41
16:15:22
11:06:59
11:03:59
11:13:38
15:43:34
11:29:35
foreach ( $total_markin_time as $time )
{
list( $g, $i, $s ) = explode( ':', $time );
//echo $g."-";
$total_markin_seconds += $g * 60 * 60;
$total_markin_seconds += $i * 60;
$total_markin_seconds += $s;
}
//echo $total_markin_seconds; exit;
$total_markin_hours = floor( $total_markin_seconds / 3600 );
$total_markin_seconds -= $total_markin_hours * 3600;
$total_markin_minutes = floor( $total_markin_seconds / 60 );
$total_markin_seconds -= $total_markin_minutes * 60;
When I am doing sum on this, I am gettin result 77:26:48
. 当我对此求和时,我得到的结果是
77:26:48
。 Where is the problem? 问题出在哪儿? Updated: Is there any problem with
floor
that I have read like, 更新:我所阅读的
floor
是否有任何问题,
The floor() function rounds a number DOWN to the nearest integer, if necessary.
There is no error and it works right. 没有错误,它正常工作。
Calculation: 计算方式:
hours: 0+16+11+11+11+15+11 = 75 hours 小时:0 + 16 + 11 + 11 + 11 + 15 + 11 = 75小时
minutes: 33+15+6+3+13+43+29 = 142 = 2 hours and 22 minutes 分钟:33 + 15 + 6 + 3 + 13 + 43 + 29 = 142 = 2小时22分钟
seconds: 41+22+59+59+38+34+35 = 288 = 4 minutes and 48 seconds 秒:41 + 22 + 59 + 59 + 38 + 34 + 35 = 288 = 4分48秒
The result of sum is 77:26:48. 总和的结果是77:26:48。
If you want to get just the sum of hours you should use something like this 如果您只想获取小时数,则应使用类似以下的内容
foreach ( $total_markin_time as $time )
{
list( $g, $i, $s ) = explode( ':', $time );
$total_markin_hours += $g;
}
It's simple. 这很简单。 All your times adds to:
您所有的时间都会增加:
288 s = 4m 48s
142 m = 2h 22m
75 h
Which in result (add hours + minutes + seconds) gives 77:26:48 结果(加时+分钟+秒)得出77:26:48
You may think that You only add hours independent of minutes and seconds, but You have to add overflows of minutes and seconds. 您可能认为您只添加了与分钟和秒无关的小时,但是您必须添加分钟和秒的溢出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.