在PHP中按小时计算OCCourance

在PHP中按小时计算OCCourance,php,mysql,date,format,substr,Php,Mysql,Date,Format,Substr,我有一个脚本可以轮询MySQL数据库Nagios,特别是事件的日期/时间。我想根据脚本返回的所有数据计算每小时发生的事件数 结果是这样的:每个人都有自己的路线 2010-03-01 03:20:26 2010-02-28 19:07:26 2010-02-28 00:50:37 2010-02-27 17:07:35 2010-02-27 17:06:35 下面是我用来获取所需内容的bash脚本: cat temp |awk '{print $2 $0}' | cut -d: -f1

我有一个脚本可以轮询MySQL数据库Nagios,特别是事件的日期/时间。我想根据脚本返回的所有数据计算每小时发生的事件数

结果是这样的:每个人都有自己的路线


2010-03-01 03:20:26
2010-02-28 19:07:26
2010-02-28 00:50:37
2010-02-27 17:07:35
2010-02-27 17:06:35
下面是我用来获取所需内容的bash脚本:


  cat temp |awk '{print $2 $0}' | cut -d: -f1 | sort | uniq -c |sort -k2 -n
以下是一些示例结果:



 21 00
     32 01
     31 02
     46 03
     34 04
     12 05
     11 06
      8 07
    107 08
     56 09
     16 10
     55 11
     50 12
     33 13
     23 14
     34 15
     11 16
     18 17
     14 18
     25 19
      9 20
      5 21
     38 22
     20 23
因此,从上面的结果可以看出,little bash命令解析我的PHP脚本的 s输出并返回第1列中出现的次数以及第2列中出现的时间

我需要将它合并到我的PHP脚本中

下面是我现在从Nagios DB检索的代码:

$query2= "SELECT * FROM nagios_statehistory WHERE ".
    "object_id='$tosearch' AND output='CRITICAL - Socket timeout " .
    "after 10 seconds' ORDER BY state_time DESC";

$result2 = mysql_query($query2) or die (mysql_error());

while($row = mysql_fetch_array($result2)) {
    $outtie = substr( $row['state_time'] , 0 , 10);
    if ($outtie !== $lastout) {
        //bolds the first occurrence of each day
        echo "<b>";
    }
    /* print the actual time.  This is what I'm looking
     * to count the occurrences of */
    echo $row['state_time'];
    $lastout = substr( $row['state_time'] , 0 , 10);
    //disable bold and insert a line break
    echo "</b><br />";
}

我需要在PHP中像bash一样,在运行当前PHP脚本的输出上运行cat temp | awk{print$2$0}| cut-d:-f1 | sort | uniq-c | sort-k2-n。上面所说的基本上就是我想要的,我用上面所说的解决了我的问题。谢谢
SELECT COUNT(*) AS `cnt`, HOUR(`state_time`) AS `hr` FROM nagios_statehistory
WHERE object_id='$tosearch' AND output='CRITICAL - Socket timeout after 10 seconds'
GROUP BY `hr`
ORDER BY state_time DESC