Does PHP have a function for aggregating time values from an array?

前端 未结 2 414
梦谈多话
梦谈多话 2021-01-26 07:37

So my query is like this:

SELECT TIMEDIFF(\'24:00:00\',(TIMEDIFF(\'22:00:00\',TIME(end)))) AS time
FROM sworkingtime
WHERE user=\'magdalena\' AND type=\'work\' A         


        
相关标签:
2条回答
  • 2021-01-26 08:15

    You can iterate the array and use this function to sum Sum time php

    0 讨论(0)
  • 2021-01-26 08:31

    No, there's no special sum function in this case - especially paying attention that you need some time summation, not just simple operation.

    To do that, you can apply callback on your array. Very powerful function for that is array_reduce(). For working with date intervals you can use DateTime API, like:

    $data = [
      ['time'=>'02:02:36'],
      ['time'=>'03:17:24'],
      ['time'=>'03:07:03']
    ];
    
    
    $time = array_reduce($data, function($c, $x)
    {
       $x = explode(':', $x['time']);
       $c = explode(':', $c);
       return (new DateTime('2000-01-01'))
          ->add(new DateInterval('PT'.(int)$c[0].'H'.(int)$c[1].'M'.(int)$c[2].'S'))
          ->add(new DateInterval('PT'.(int)$x[0].'H'.(int)$x[1].'M'.(int)$x[2].'S'))
          ->format('H:i:s');
    }, '00:00:00');
    

    -this will result in "08:27:03" as string. But if you want to get object, remove format() call.

    0 讨论(0)
提交回复
热议问题