(float) $latest['value'], 'hourAgo' => (float) $hourAgo['value'], 'elapsedH' => max( 1, (strtotime($latest['timestamp']) - strtotime($hourAgo['timestamp'])) / 3600 ), ]; } $east = fetchLatestAndHourAgo($con, 'easttotalground', 'easttotground'); $west = $con2 ? fetchLatestAndHourAgo($con2, 'westtotalground', 'MODBUSENET_TABLE900_WTOTGROUND_VALUE') : null; $totalToday = (float) mysqli_fetch_column(mysqli_query( $con, "SELECT MillTotalGround FROM mill_total_ground ORDER BY id DESC LIMIT 1" )); mysqli_close($con); if ($con2) { mysqli_close($con2); } $hoursSinceFive = (time() < strtotime('5:00')) ? (time() + 86400 - strtotime('5:00')) / 3600 : (time() - strtotime('5:00')) / 3600; $hoursRemaining = max(0, 24 - $hoursSinceFive); $eastProjected = $east ? ($east['latest'] - $east['hourAgo']) / $east['elapsedH'] * $hoursRemaining : 0; $westProjected = $west ? ($west['latest'] - $west['hourAgo']) / $west['elapsedH'] * $hoursRemaining : 0; echo number_format($totalToday + $eastProjected + $westProjected, 0);