php 如何求和 N 次(HH:MM 格式)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22681725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sum N number of time (HH:MM Format)?
提问by user3239311
I am using the following sample code to calculate sum of two different time values. Now I want to get the sum of N number of time values.
我正在使用以下示例代码来计算两个不同时间值的总和。现在我想得到 N 个时间值的总和。
// numbers for testing
$o="12:59";
$p="0:58";
// display for testing
echo "$o<br />";
echo "$p<br />";
echo AddPlayTime($o,$p);
// FUNCTION - ADD HOURS and MINUTES
function AddPlayTime ($oldPlayTime, $PlayTimeToAdd) {
$old=explode(":",$oldPlayTime);
$play=explode(":",$PlayTimeToAdd);
$hours=$old[0]+$play[0];
$minutes=$old[1]+$play[1];
if($minutes > 59){
$minutes=$minutes-60;
$hours++;
}
if($minutes < 10){
$minutes = "0".$minutes;
}
if($minutes == 0){
$minutes = "00";
}
$sum=$hours.":".$minutes;
return $sum;
}
回答by ponciste
this should do what you are looking for:
这应该做你正在寻找的:
$times
is the array of times and you can add how many time you want
$times
是时间数组,您可以添加所需的时间
$times = array();
$times[] = "12:59";
$times[] = "0:58";
$times[] = "0:02";
// pass the array to the function
echo AddPlayTime($times);
function AddPlayTime($times) {
$minutes = 0; //declare minutes either it gives Notice: Undefined variable
// loop throught all the times
foreach ($times as $time) {
list($hour, $minute) = explode(':', $time);
$minutes += $hour * 60;
$minutes += $minute;
}
$hours = floor($minutes / 60);
$minutes -= $hours * 60;
// returns the time already formatted
return sprintf('%02d:%02d', $hours, $minutes);
}
EDIT
编辑
I edited the code with the right names of the variables. It is more correct now.
我用正确的变量名称编辑了代码。现在更正确了。
hope this helps :-)
希望这可以帮助 :-)
回答by Glavi?
Here is an function that will sum all your time valuesin format HH:MM
:
这是一个函数,它将以格式对所有时间值求和HH:MM
:
function sum_time() {
$i = 0;
foreach (func_get_args() as $time) {
sscanf($time, '%d:%d', $hour, $min);
$i += $hour * 60 + $min;
}
if ($h = floor($i / 60)) {
$i %= 60;
}
return sprintf('%02d:%02d', $h, $i);
}
// use example
echo sum_time('01:05', '00:02', '05:59'); # 07:06
回答by William
function sumarHoras($acumuladoTime, $nuevoTime){
//Se esperan parametros así:
//$acumuladoTime="02:45";
//$nuevoTime="04:36";
//echo "Hora acumulada: $acumuladoTime"."<br>";
//echo "Nuevo tiempo acumulado: $nuevoTime"."<br>";
/*Tiempo acumulado*/
$myArrayAcumuladoTime=explode(":", $acumuladoTime);
$hrsAcumuladoTime=$myArrayAcumuladoTime[0];
$minsAcumuladoTime=$myArrayAcumuladoTime[1];
/*Nuevo Time*/
$myArrayNewTime=explode(":", $nuevoTime);
$hraNewTime=$myArrayNewTime[0];
$minNewTime=$myArrayNewTime[1];
/*Calculo*/
$sumHrs=$hrsAcumuladoTime+$hraNewTime;
$sumMins=$minsAcumuladoTime+$minNewTime;
/*Si se pasan los MINUTOS*/
if($sumMins>59){
/*Quitamos hora para dejarlo en minutos y se la sumamos a la de horas*/
$sumMins-=60;
$sumHrs+=1;
}
// echo "Total hrs agregadas: $sumHrs:$sumMins"."<br>";
return "$sumHrs:$sumMins";
}
回答by Ramas Win
This is the best way:
这是最好的方法:
<?php
function CalculateTime($times) {
$i = 0;
foreach ($times as $time) {
sscanf($time, '%d:%d', $hour, $min);
$i += $hour * 60 + $min;
}
if($h = floor($i / 60)) {
$i %= 60;
}
return sprintf('%02d:%02d', $h, $i);
}
$date[] = '02:32';
$date[] = '01:29';
echo CalculateTime($date);
?>
回答by Rudra Shekhar Venkateshwarlu
Laravel Framework (PHP language)
Laravel 框架(PHP 语言)
$chores = ChoresTime::where('family_id', $family->id)->get();
$total = [];
foreach ($chores as $key => $value) {
$total[] = $value->time;
}
$total = CalculateTime($chores);
return response()->json([
'status' => 1,
'message' => 'Total Time',
'data' => [],
'total' => $total
], ok());
Answer:
回答:
{
"status": 1,
"message": "Family Total Chores Time",
"data": [],
"total": "14:22"
}