2016-10-23 4 views
1

Например, у меня есть несколько раз, как:Как я могу суммировать несколько раз в Laravel 5,2

$a = "9:00:00"; 
$b = "8:00:00"; 
$c = "9:00:00"; 

и так далее ...

Он должен вернуться в 26:00:00, но как вычесть его с 45:00:00?

Мне нужно узнать общее рабочее время и сверхурочное время для моего посещения.

+1

это только когда-либо будет полный день или есть изменение, которое вы могли бы иметь '«8:30:00»'? –

ответ

0

Углерод - это то, что вам нужно, оно интегрируется с laravel по умолчанию.

http://carbon.nesbot.com/docs/

используя его addHours() и subHours() функцию для достижения ваших требований.

0

Вы можете сделать следующее:

$sumSeconds = 0; 
    foreach($times as $time) { 
     $explodedTime = explode(':', $time); 
     $seconds = $explodedTime[0]*3600+$explodedTime[1]*60+$explodedTime[2]; 
     $sumSeconds += $explodedTime; 
    } 
    $hours = floor($sumSeconds/3600); 
    $minutes = floor(($sumSeconds % 3600)/60); 
    $seconds = (($sumSeconds%3600)%60); 
    $sumTime = $hours.':'.$minutes.':'.$seconds; 

Это код Подводя три раза (предполагается, что они находятся в массиве) и код для вычитания будет почти такой же, но для вычитания вы будете вычитайте $sumSeconds обоих раз, а затем преобразуйте результат.

2

Вы можете найти различные варианты, используя Carbon. Чтобы добавить эти времена, нужно немного уловки. Вы можете сделать это следующим образом:

$a = '09:00:00'; 
$b = '08:00:00'; 
$c = '09:00:00'; 

//convert the $a in carbon instance. 
//convert $b and $c in integer, you can add only integer with carbon. 
$d = Carbon::createFromFormat('H:i:s',$a)->addHours(intval($b))->addHours((intval($c))); 

//convert the time "45:00:00" to carbon 
$e = Carbon::createFromFormat('H:i:s','45:00:00'); 

//return the difference 
$e->diffInHours($d) 
0
$times = [ 
    "9:00:00", 
    "8:00:00", 
    "9:00:00", 
]; 

// Converting the time to seconds makes calculations 
// more simple and easier to understand. 
function timeToSeconds($time) { 
    list($hours, $minutes, $seconds) = explode(":", $time); 

    return ($hours * 60 * 60) + ($minutes * 60) + $seconds; 
} 


// Let's use this to convert say 300s into 00:05:00 
function formatSecondsAsHMI($seconds) { 
    return sprintf(
    '%02d:%02d:%02d', 
    floor($seconds/3600), 
    floor($seconds/60 % 60), 
    floor($seconds % 60) 
); 
} 

// Add an array of times together and return the formatted string hh:mm:ss 
function addTimes($times) { 
    $seconds = array_sum(array_map(function ($time) { 
    return timeToSeconds($time); 
    }, $times)); 

    return formatSecondsAsHMI($seconds); 
} 

// Subtract an array of times. Order of array important. 
// Subtracts 0 from 1 from 2 where 0,1,2 are array keys 
// i.e. [03:00:00, 10:00:00] would subtract 3 from 10 = 07:00:00 
function subtractTimes($times) { 
    $times = array_map(function($time) { 
    return timeToSeconds($time); 
    }, $times); 

    return array_reduce($times, function($carry, $item) { 
    return ($item - $carry); 
    }); 
} 

// Now just add the times together and subtract the result from 45 
echo subtractTimes([addTimes($times), '45:00:00']); 
Смежные вопросы