• 1 Post
  • 4 Comments
Joined 2 years ago
cake
Cake day: June 20th, 2023

help-circle
  • The mentioned php file:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Charging Time Calculator (PHP)</title>
      <style>
        body { font-family: Arial, sans-serif; background: #f4f4f4; color: #333; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
        .box { background: #fff; padding: 20px 30px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); width: 100%; max-width: 400px; }
        h1 { margin-top: 0; text-align: center; }
        .field { margin-bottom: 15px; }
        label { display: block; margin-bottom: 5px; font-weight: bold; }
        input[type="number"] { width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; }
        button { width: 100%; padding: 10px; background: #28a745; color: white; border: none; border-radius: 4px; cursor: pointer; }
        button:hover { background: #218838; }
        .result { margin-top: 20px; background: #e9ecef; padding: 15px; border-radius: 4px; }
      </style>
    </head>
    <body>
      <div class="box">
        <h1>Charging Time Calculator</h1>
    
        <?php
        // Default values at the top
        $default_capacity      = 52.0;   // kWh
        $default_current_pct   = 55.0;   // %
        $default_target_pct    = 80.0;   // %
        $default_power_kw      = 1.8;    // kW
        $default_loss_pct      = 15.2;   // Charging loss in %
    
        // I have extended the PHP script with the new field for charging loss (default 15.2%). 
        // The loss is applied immediately after calculating the net energy requirement, 
        // so the charging time is determined based on the inclusive energy requirement (net + loss). 
        // Additionally, the result view now shows the following values:
        // - Net energy required
        // - Configured charging loss
        // - Energy requirement including loss
    
        // Take from POST or use defaults
        $capacity = isset($_POST['capacity']) ? floatval($_POST['capacity']) : $default_capacity;
        $current  = isset($_POST['current'])  ? floatval($_POST['current'])  : $default_current_pct;
        $target   = isset($_POST['target'])   ? floatval($_POST['target'])   : $default_target_pct;
        $power    = isset($_POST['power'])    ? floatval($_POST['power'])    : $default_power_kw;
        $loss     = isset($_POST['loss'])     ? floatval($_POST['loss'])     : $default_loss_pct;
    
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
          // Calculation
          $percent_to_charge = max(0, $target - $current);
          // Net energy requirement
          $energy_needed_raw = ($percent_to_charge / 100) * $capacity;      // in kWh
          // Energy requirement including charging loss
          $energy_needed = $energy_needed_raw * (1 + $loss / 100);
          // Charging time based on the increased energy requirement
          $time_hours        = $power > 0 ? $energy_needed / $power : 0;    // in hours
          $hours             = floor($time_hours);
          $minutes           = round(($time_hours - $hours) * 60);
        ?>
          <div class="result">
            <p><strong>Remaining Charging Time:</strong> <?= $hours ?> h <?= $minutes ?> min</p>
            <p><strong>Percent to Charge:</strong> <?= number_format($percent_to_charge, 1) ?> %</p>
            <p><strong>Energy to Charge (net):</strong> <?= number_format($energy_needed_raw, 2) ?> kWh</p>
            <p><strong>Charging Loss:</strong> <?= number_format($loss, 1) ?> %</p>
            <p><strong>Energy to Charge (incl. loss):</strong> <?= number_format($energy_needed, 2) ?> kWh</p>
          </div>
        <?php } ?>
    
        <form method="post">
          <div class="field">
            <label for="capacity">Battery Capacity (kWh)</label>
            <input type="number" step="0.1" name="capacity" id="capacity" value="<?= htmlspecialchars($capacity) ?>" required>
          </div>
          <div class="field">
            <label for="current">Current Charge Level (%)</label>
            <input type="number" step="1" min="0" max="100" name="current" id="current" value="<?= htmlspecialchars($current) ?>" required>
          </div>
          <div class="field">
            <label for="target">Desired Charge Level (%)</label>
            <input type="number" step="1" min="0" max="100" name="target" id="target" value="<?= htmlspecialchars($target) ?>" required>
          </div>
          <div class="field">
            <label for="power">Charging Power (kW)</label>
            <input type="number" step="0.1" min="0.1" name="power" id="power" value="<?= htmlspecialchars($power) ?>" required>
          </div>
          <div class="field">
            <label for="loss">Charging Loss (%)</label>
            <input type="number" step="0.1" min="0" max="100" name="loss" id="loss" value="<?= htmlspecialchars($loss) ?>" required>
          </div>
          <button type="submit">Calculate</button>
        </form>
      </div>
    </body>
    </html>