gpt4 book ai didi

php - 来 self 的数据库的每日报告

转载 作者:行者123 更新时间:2023-11-29 21:46:18 24 4
gpt4 key购买 nike

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

<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 - 来 self 的数据库的每日报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34084046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com