What are some alternative approaches to calculating and displaying summarized data from a specific time frame in PHP using SQL queries?

When calculating and displaying summarized data from a specific time frame in PHP using SQL queries, one alternative approach is to use the GROUP BY clause in the SQL query to group the data by a specific time interval, such as day, week, or month. This allows for easy aggregation of data within the specified time frame. Additionally, using functions like SUM, COUNT, AVG, etc., in the SELECT statement can help to calculate and display summarized data effectively.

<?php

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// SQL query to calculate and display summarized data for a specific time frame
$sql = "SELECT DATE_FORMAT(date_column, '%Y-%m-%d') AS date, SUM(amount_column) AS total_amount
        FROM table_name
        WHERE date_column BETWEEN 'start_date' AND 'end_date'
        GROUP BY DATE_FORMAT(date_column, '%Y-%m-%d')";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Date: " . $row["date"]. " - Total Amount: " . $row["total_amount"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

?>