php和mysql显示date_开始和date_end之间的每日收入

f87krz0w  于 2022-12-02  发布在  PHP
关注(0)|答案(3)|浏览(145)

我有一个表,看起来像这样:

$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     |
+---------------------+--------+

对于这个例子,表每天正好有4条记录,但是真实的的表每天可以有更多或更少的记录。
我需要的是一个列表的所有天之间的某个时间跨度,和金额的总和,即收入在每一天。
示例:
输入:

$date_begin = '2019-05-02 00:00:00';
$date_end = '2019-05-04 23:59:59';

对于这些参数,脚本应输出以下内容:

2019-05-02
25

2019-05-03
55

2019-05-04
40

第二(不是很重要,请关注第一个任务):显示每一天的星期名称会很有帮助。我想我必须使用一个库来实现这一点。或者php本身就能够确定过去日期的星期名称吗?
因此,它看起来像这样:

2019-05-02
Thursday
25

2019-05-03
Friday
55

2019-05-04
Saturday
40

我真的试着先自己解决它,并在寻找例子。但我发现的每一个都要么比我需要的更复杂,要么太不同了,我无法正确地采用它。
谢谢

编辑:

我尝试了回复中的一个建议。但我得到的只是一张空白页。谁能告诉我我错在哪里?

<?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>

保安的事先不要担心,我会处理好的。

kninwzqo

kninwzqo1#

这个查询看起来有点像你描述你想要什么。你没有显示任何代码或指定MySQL API,所以请看这里查询How can I prevent SQL injection in PHP?的安全方法:

SELECT DATE(`date`), SUM(`amount`) FROM $table_name
    WHERE `date` BETWEEN '$date_begin' AND '$date_end'
    GROUP BY DATE(`date`) ORDER BY `date`

然后,在获取行和输出时,只需使用date('l')来获取日期的文本表示。

tvokkenx

tvokkenx2#

首先在SQL中运行以下代码:问题是您的开始和结束日期。您分配了月份06,但数据库包含月份为05的日期。

set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

现在将此代码放入您的文件:

<?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-01 00:00:00';
    $date_end = '2019-05-02 23:59:59';

    $sql = "SELECT DATE(`date`) as date, SUM(`amount`) as amount FROM transaction WHERE (date > '$date_begin' AND date < '$date_end') GROUP 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>".date("l", strtotime($row['date'])).'<br>'.$row['amount'].'<br><br>';

    }

    ?>

    </body>
    </html>
dphi5xsq

dphi5xsq3#

请试试这个

$db = mysqli_connect($dbserver, $dbuser, $dbpw, $dbname);

 $date_begin = '2019-05-02 00:00:00';
 $date_end = '2019-05-05 23:59:59';
 
 mysqli_query($db, "SELECT SUM(amount) AS totaltransactions FROM 
 transactions WHERE date  BETWEEN 
'$date_begin' AND '$date_end'");

 $row = mysqli_fetch_assoc($result);

 echo  $sumTransactions = $row['totaltransactions'];

相关问题