I use the same query to return one integer value between two dates which is a sum of column
SQL Query:
SELECT SUM(TransactionAmount) FROM `sales` WHERE TransactionTime BETWEEN '2020-12-15' AND '2020-12-16'
In this PHP Code:
<?php
require 'config/db.php';
if(isset($_POST["searchinDate-btn"])){
$start_date = $_POST['startdate'];
$end_date = $_POST['enddate'];
$totalRev = "SELECT SUM(TransactionAmount) FROM `sales` WHERE TransactionTime BETWEEN '$start_date' AND '$end_date'";
$totalRevQ = mysqli_query($conn, $totalRev);
$rev = mysqli_fetch_assoc($totalRevQ);
$revenue = $rev[0];
}
?>
(Additional) HTML Code:
<form action="Dashboard.php" method="POST">
<div class="dateselect">
<label for="start-date">Start Date: </label>
<input type="date" id="start-date" name="startdate" value="">
<label for="start-date"> - - End Date: </label>
<input type="date" id="end-date" name="enddate" value="">
<button style="margin-bottom: 3px;" type="submit" name="searchinDate-btn" class="mbtn btn btn-primary">Show</button>
</div>
</form>
<p> <?php echo $revenue; ?> </p>
The Variable revenue return nothing for some reason! Note: Both start date and end date and the query result actually return results but I think there is a problem with showing the result of the query which will be one integer value like 562 or something else! ( I need the value of the row number 0 not the count of the rows)
UPDATE: The wrong was in passing date type from PHP to compare it with timestamp field type at the database.
to convert date to time stamp I use this PHP Code:
<?php
require 'config/db.php';
if(isset($_POST["searchinDate-btn"])){
$start_date = $_POST['startdate'];
$StartDate = date("Y-m-d H:i:s", strtotime($start_date));
$end_date = $_POST['enddate'];
$EndDate = date("Y-m-d H:i:s", strtotime($end_date + '24 hours'));
$totalRev = "SELECT SUM(TransactionAmount) AS Revenue FROM `sales` WHERE TransactionTime BETWEEN '$StartDate' AND '$EndDate'";
$totalRevQ = mysqli_query($conn, $totalRev);
$rev = mysqli_fetch_assoc($totalRevQ);
$revenue = $rev['Revenue'];
}
?>