PHP是否有从数组聚集时间值的函数? [英] Does PHP have a function for aggregating time values from an array?

查看:129
本文介绍了PHP是否有从数组聚集时间值的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的查询是这样的:

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' AND start BETWEEN '2014-03-01' AND '2014-03-28' AND (HOUR(`end`) > 22 OR HOUR(`end`) < 4)
AND completed=1

我的查询返回这样的:

My query returns this:

02:02:36
03:17:24
03:07:03
02:24:17
03:14:09

有没有修改该查询返回本场的唯一SUM的一种方式,所以只?一个字段

Is there a way to modify this query to return only SUM of this fields, so one field only?

感谢您

推荐答案

没有,有一个在这种情况下,没有特殊的和函数 - 尤其是付出,你需要一些时间总和,而不仅仅是简单的操作注意

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

要做到这一点,您可以在阵列上应用的回调。对于非常强大的功能是 array_reduce() 。凡是注日期的时间间隔,则可以使用日期时间的API,如:

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');

- 这将导致8时27分03秒的字符串。但是,如果你想要得到的对象,删除格式()电话。

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

这篇关于PHP是否有从数组聚集时间值的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆