gpt4 book ai didi

php - 计算每个月有多少事件

转载 作者:行者123 更新时间:2023-11-29 04:38:59 26 4
gpt4 key购买 nike

我有一个图表,我想在其中填写有关我每月有多少条记录的信息。图表需要以这种格式接收数据:

    data: [
["Jan", 140],
["Feb", 240],
["Mar", 190],
["Apr", 140],
["May", 180],
["Jun", 320],
["Jul", 270],
["Aug", 180]
],

我需要生成我每个月有多少条记录。我的数据库看起来像:

id |   start     |    end     |
------------------------------
1 | 2015-10-01 | 2015-10-31 |
2 | 2015-10-05 | 2015-10-09 |
3 | 2015-10-06 | 2015-10-07 |
4 | 2015-11-01 | 2015-11-02 |
5 | 2015-11-03 | 2015-11-06 |
6 | 2015-11-09 | 2015-10-13 |
7 | 2015-11-16 | 2015-10-18 |
8 | 2015-11-21 | 2015-10-29 |

所以我们在 10 月有 3 条记录,在 11 月有 5 条记录。我应该如何正确初始化一个函数,该函数最终会返回我可以传递给数据的正确格式?我可以获取像

这样的记录

型号:

   function specialtest($start, $end) {
$q = ("SELECT count(*) as count FROM reservations WHERE end > '$start' and start < '$end'");
$query = $this->db->query($q);
print json_encode($query->result_array());
exit;
}

然后在 Controller 中:

   $data = $this->model->specialtest($arg, $arg2);

但这并不是我真正需要的。我每个月都需要这些记录而不调用任何函数参数,因为我不知道它们?有什么想法吗?

最佳答案

只要此函数未在应用的其他地方使用,请将查询更新为如下所示:

$q = "SELECT DATE_FORMAT(start, '%b') as month, DATE_FORMAT(start, '%Y') as year, count(*) as count FROM reservations WHERE end > '$start' and start < '$end' GROUP BY DATE_FORMAT(start, '%b-%Y')";

请注意,您需要包括年份,这样明年您就不会获取前一年的条目。

关于php - 计算每个月有多少事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33894702/

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