我使用下面的示例代码来计算两个不同时间值的和。现在我想要得到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;
}
这应该是你正在寻找的:
$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);
}
编辑
我用正确的变量名编辑了代码。希望有帮助:-)
下面是一个函数,它将以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
演示
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";
}
这是最好的方法:
<?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);
?>
Laravel Framework (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());
答:
{
"status": 1,
"message": "Family Total Chores Time",
"data": [],
"total": "14:22"
}
我已经将上面的sum_time函数扩展为秒:
function sum_time()
{
$hh = 0;
$mm = 0;
$ss = 0;
foreach (func_get_args() as $time)
{
sscanf( $time, '%d:%d:%d', $hours, $mins, $secs);
$hh += $hours;
$mm += $mins;
$ss += $secs;
}
$mm += floor( $ss / 60 ); $ss = $ss % 60;
$hh += floor( $mm / 60 ); $mm = $mm % 60;
return sprintf('%02d:%02d:%02d', $hh, $mm, $ss);
}