简体   繁体   English

如何计算事件的总时间(以分钟计),在一小时内发生了很多次,在Msql和php中每个小时

[英]How to calculate total time in minuets of an event occurred many time within an hour and each hour in Msql and php

I am hanged in a problem may be due to my lack of expertise. 我陷入困境可能是由于我缺乏专业知识。 I have following mysql table: 我有以下mysql表:

Fdname -- fcode - timestate - Status FDNAME - fcode- 时间 状态 - 状态

University 104 2015-03-05 01:37:00 OFF 大学104 2015-03-05 01:37:00关闭

University 104 2015-03-05 01:42:00 ON 大学104 2015-03-05 01:42:00 ON

University 104 2015-03-05 01:55:00 OFF 大学104 2015-03-05 01:55:00关闭

University 104 2015-03-05 02:00:00 ON 大学104 2015-03-05 02:00:00 ON

University 104 2015-03-05 07:20:00 OFF 大学104 2015-03-05 07:20:00 OFF

University 104 2015-03-05 07:22:00 ON 大学104 2015-03-05 07:22:00 ON

University 104 2015-03-05 10:17:00 OFF 大学104 2015-03-05 10:17:00关闭

University 104 2015-03-05 11:16:00 ON 大学104 2015-03-05 11:16:00 ON

University 104 2015-03-05 12:17:00 OFF 大学104 2015-03-05 12:17:00关闭

University 104 2015-03-05 13:17:00 ON 大学104 2015-03-05 13:17:00 ON

University 104 2015-03-05 15:19:00 OFF 大学104 2015-03-05 15:19:00关闭

University 104 2015-03-05 16:16:00 ON 大学104 2015-03-05 16:16:00 ON

University 104 2015-03-05 18:16:00 OFF 大学104 2015-03-05 18:16:00关闭

University 104 2015-03-05 20:16:00 ON 大学104 2015-03-05 20:16:00 ON

University 104 2015-03-05 21:17:00 OFF 大学104 2015-03-05 21:17:00关闭

University 104 2015-03-05 22:18:00 ON 大学104 2015-03-05 22:18:00 ON

I need to calculate cumulative total minutes for each state 'OFF' and 'ON' per clock hour. 我需要计算每个时钟小时“ OFF”和“ ON”每个状态的累积总分钟数。 ie i should know how many minutes a fname remains OFF within each hour. 即我应该知道一个小时内一个fname保持关闭状态的分钟数。 I tried a lot in php but could find any clue. 我在php中尝试了很多,但可以找到任何线索。 ANy body can help or suggest in terms of query or php script. 任何机构都可以根据查询或php脚本提供帮助或建议。 I will really appreciate. 我会很感激。

Considering that $rs is the result of your appropriate query, I believe this will give you the result you're looking for. 考虑到$rs是您适当查询的结果,我相信这将为您提供所需的结果。

$ontime = NULL;
$offtime = NULL;
$totalseconds = 0;

while($r = mysql_fetch_array($rs)){ 
    if($r["status"] == "OFF"){
        $offtime = strtotime($r["timestate"]);
    }
    if($r["status"] == "ON" AND $offtime !== NULL){
        $ontime = strtotime($r["timestate"]);
        $totalseconds += $ontime - $offtime;
        $offtime=NULL;
        $ontime=NULL; 
    }
}

$totalseconds will hold your total downtime in seconds $totalseconds将以秒为单位保存您的总停机时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM