我有一个名为it_outage的表,如下所示:
office | start | end | total
------------------------------------------------------------
nyc | 2014-07-01 20:56:00 | 2014-07-01 22:19:00 | 01:23 |
ewi | 2014-07-17 17:35:00 | 2014-07-17 18:05:00 | 00:30 |
nyc | 2014-07-11 03:09:00 | 2014-07-11 03:26:00 | 00:17 |
------------------------------------------------------------所以,基本上它只是显示了办公室何时停工,以及它持续了多长时间。我如何计算“纽约”办公室的总时间?因此,我希望输出为01:40 (合并01:23 + 00:17)。
我的查询如下(我使用Joomla 3.3):
$query->select($db->quoteName(array('id', 'office', 'start', 'end', 'total')));
$query->from($db->quoteName('#__outage'));发布于 2014-07-19 14:03:55
使用Joomla对象尝试这个mySQL:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select("office,`start`,`end`,SEC_TO_TIME( SUM((UNIX_TIMESTAMP(`end`) - UNIX_TIMESTAMP(`start`))) ) AS total_outage");
$query->from($db->quoteName('#__outage'));
$query->group('office');
$db->setQuery($query);
foreach($result= $db->loadObjectList() as $outage) {
echo $outage->office . " : " . $outage->total_outage . '<Br/>';
}给出如下内容:
ewi : 00:30:00
nyc : 01:40:00发布于 2014-07-18 13:18:34
您必须将查询结果放到下面的代码中。
$i=0;
foreach( $results as $row)
{
if($i == 0 ){
$dt = DateTime::createFromFormat('H:i', $row['total']);
}
$arr = explode(':',$row['total']);
$dt->add(new DateInterval('PT'.$arr[0].'H'));
$dt->add(new DateInterval('PT'.$arr[1].'M'));
$i++;
}
/* Total time */
echo $dt->format('H:i');https://stackoverflow.com/questions/24824047
复制相似问题