如何在laravel 5.2中多次累计

时间:2016-10-23 07:34:52

标签: php laravel-4 laravel-5 laravel-5.2 laravel-5.1

例如,我有多次像:

No such property: COMPILE_SDK for class: org.gradle.api.Project

依旧......

必须在26:00:00返回但是如何从45:00:00减去它?

我必须找出出勤的总工时和加班费。

4 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

碳是你需要的,它默认与laravel集成。

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

使用其addHours()subHours()功能来满足您的要求。

答案 2 :(得分: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然后转换结果

答案 3 :(得分: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']);