【问题标题】:How can I sum multiple time in laravel 5.2如何在 laravel 5.2 中多次求和
【发布时间】:2016-10-23 07:34:52
【问题描述】:

例如,我有多次喜欢:

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

等等……

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

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

【问题讨论】:

  • 是只有完整的小时数还是你可以有改变"8:30:00"

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


【解决方案1】:

您可以使用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)

【讨论】:

    【解决方案2】:

    您可以执行以下操作:

      $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】:

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

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

      使用其addHours()subHours() 函数来实现您的要求。

      【讨论】:

        【解决方案4】:
        $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']);
        

        【讨论】:

          猜你喜欢
          • 2016-07-07
          • 2016-11-12
          • 2016-04-20
          • 2017-11-02
          • 1970-01-01
          • 2017-03-11
          • 2016-07-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多