1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!--?php
    $previousHour = date('Y-m-d H', strtotime('-1 hour'));
    $previousHourFull = $previousHour.":00:00";
    $theHour = $previousHour;
    $watts = "solarWatts";
 
    $pdo = new PDO('mysql:host=localhost;dbname=xxxxxxx', "xxxxxxx", "xxxxxxx");
    $pdo--->query("SET NAMES utf8");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
    $readingsQuery = $pdo->query("SELECT time,$watts FROM log WHERE time LIKE '$theHour:%'");
    foreach($readingsQuery->fetchAll() as $row) {
        for($i=0;$i<60;$i++){
            if ($i < 10){
                $minuteString = "0".$i;
            }else{
                $minuteString = $i;
            }
            if (strpos($row['time'],$theHour.':'.$minuteString.':') !== false){
                $minute[$i][] = $row[$watts];
            }
        }
    }
    for ($v=0;$v<60;$v++){
        $hour[$v] = round(((array_sum($minute[$v])/count($minute[$v]))/60)/1000,8);
    }
 
    $kwh = round(array_sum($hour),4);
    if ($pdo->query("INSERT INTO kwh_generated VALUES ('','$previousHourFull','$kwh')")){
        echo "Added $kwh kwh for hour $previousHourFull to kwh_generated table";
    }
?>