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"; } ?>