I have a table which looks kind of like this:
$table_name = 'transactions';
+---------------------+--------+
| date | amount |
+---------------------+--------+
| 2019-05-01 03:11:34 | 5 |
| 2019-05-01 04:26:26 | 10 |
| 2019-05-01 07:27:34 | 10 |
| 2019-05-01 19:22:47 | 20 |
| 2019-05-02 00:42:10 | 5 |
| 2019-05-02 04:27:31 | 10 |
| 2019-05-02 05:14:03 | 5 |
| 2019-05-02 08:39:24 | 5 |
| 2019-05-03 12:45:29 | 15 |
| 2019-05-03 12:48:23 | 15 |
| 2019-05-03 13:50:14 | 5 |
| 2019-05-03 16:58:08 | 20 |
| 2019-05-04 01:27:28 | 10 |
| 2019-05-04 11:53:15 | 20 |
| 2019-05-04 15:48:51 | 5 |
| 2019-05-04 15:59:48 | 5 |
| 2019-05-05 01:55:40 | 10 |
| 2019-05-05 05:32:53 | 15 |
| 2019-05-05 11:56:56 | 15 |
| 2019-05-05 12:09:26 | 10 |
+---------------------+--------+
For this example the table has exactly 4 records per day. But the real table can have more or less per day of course.
What I need is a list of all days between a certain time span, and the sum of amount i.e. income at each day.
Example:
Input:
$date_begin = '2019-05-02 00:00:00';
$date_end = '2019-05-04 23:59:59';
For these parameters the script should output the following:
2019-05-02
25
2019-05-03
55
2019-05-04
40
Secondly (not so important. Please focus on the first task): It would be helpful to display the name of the week day of each day. I guess I would have to use a library for that. Or is php natively able to determine the day-name of past dates?
So, it would look like this:
2019-05-02
Thursday
25
2019-05-03
Friday
55
2019-05-04
Saturday
40
I really tried to solve it for myself first and was searching for examples. But each I found was either more complicated than what I needed, or too different for me to be able to adopt it properly.
Thanks
EDIT:
I tried one of the suggestions in the replies. But all I get is a blank page. Can anyone tell me where I am wrong?
<?php
$conn = mysqli_connect($dbserver, $dbuser, $dbpw, $dbname) or die("Connection failed: " . mysqli_connect_error());
$conn->query("SET NAMES 'utf8'");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$table_name = 'transactions';
$date_begin = '2019-05-02 00:00:00';
$date_end = '2019-05-05 23:59:59';
$sql = "SELECT DATE(`date`), SUM(`amount`) FROM $table_name WHERE `date` BETWEEN '$date_begin' AND '$date_end' GROUP BY DATE(`date`) ORDER BY `date`";
$rs_result = mysqli_query($conn, $sql);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Income daily</title>
<meta name="robots" content="noindex, nofollow">
</head>
<body>
<?php
while ($row = mysqli_fetch_assoc($rs_result)) {
echo '
'.$row['date'].'
<br>
'.$row['amount'].'
<br><br>
';
}
?>
</body>
</html>
Please don't worry about the security for now. I will take care of that afterwards.