大家好,我从数据库中生成每日报告,并将其显示在表格中。我的代码是:

<table class="table table-striped m-b-none" id="stats">
    <thead>
        <tr>
            <th>Date</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>
    <?php
    try
    {
    if($_POST["submit"]{
        $stmt = $DB_con->prepare("SELECT Date(time) AS date, COUNT(*) AS total FROM branches GROUP BY date;");
        $stmt->execute();
            foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row){
                $date = $row['date'];
                $count = $row['total'];

    ?>
    <tr>
        <td><?php echo $date; ?></td>
        <td><?php echo $count; ?></td>
    </tr>
    <?php
            }
    }
    $conn=null;
    }catch(PDOException $e){
        echo "error  " . $e->getMessage();
    }
    ?>
    </tbody>
</table>


我得到的输出是:


2015/10/26:10
2015/10/27:5


但是我不仅要有记录的那一天,还希望一个月中的每一天。我希望我的桌子是这样的:


2015/10/26:10
2015/10/27:5
2015/10/28:0
2015/10/29:0
2015/10/30:0
等等..


我该怎么做?

谢谢!

最佳答案

您可以通过循环使用日期来实现。使用日期作为增量。

<?php

    // Start date
    $date = '2009-12-06';
    // End date
    $end_date = '2020-12-31';

    while (strtotime($date) <= strtotime($end_date)) {
                echo "$date\n";
                $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
    }

?>


这将从您要开始的日期到结束日期打印日期。

根据您的代码,我建议您创建一个像

function increaseDate($date = null){
   return $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}


并从循环内部调用它。阅读Php Date以了解日期格式。

关于php - 我数据库中的每日报告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34084046/

10-14 16:20
查看更多