我正在尝试使用SLIM框架从MYSQL数据库获得基于月份的项目总数,当我显示数据时我得到结果但是即使在同一个月有分离。我的代码是,
$app->get('/view/test', function ($request, $response, array $args) {
try {
$con = $this->db;
date_default_timezone_set('UTC+3h');
$sql = "SELECT start_time,COUNT(*) as tt FROM parking_sessions GROUP
BY FROM_UNIXTIME(start_time/1000)";
$result = null;
$results = null;
foreach ($con->query($sql) as $row) {
$result['muda'] = date('M',($row['start_time']/1000));
$result['total'] = $row['tt'];
$results[] =$result;
$total = count($results);
}
if ($result) {
return $response->withJson(array('status' => 'true', 'date' =>
$results, 'jumla' => $total), 200);
} else {
return $response->withJson(array('status' => 'Owner Not Found'),
422);
}
} catch (\Exception $ex) {
return $response->withJson(array('error' => $ex->getMessage()), 422);
}
});
它给了我以下结果
"date": [
{
"month": "Feb",
"total": "1"
},
{
"month": "Mar",
"total": "1"
},
{
"month": "Apr",
"total": "1"
},
{
"month": "Apr",
"total": "1"
}
],
所有结果都需要如下
"date": [
{
"month": "Feb",
"total": "1"
},
{
"month": "Mar",
"total": "1"
},
{
"month": "Apr",
"total": "2"
}
],
而我的数据库看起来像
id slot_id customer_vehicle_id start_time end_time check_out_time status
1 1 2 1553249524000 1554346841515 1553111000 1
2 6 4 1554351619803 1553253124000 1553214094 0
3 6 4 1555224715000 1553253124000 1553214094 0
4 7 3 5086800000 1553253124000 1553214094 0
您不能使用unixtime按查询分组,因为您在unix时间内获得每个不同的秒和分钟。你需要的是没有时间将它转换为日期。 SELECT start_time,COUNT(*)as tt FROM parking_sessions GROUP BY DATE(FROM_UNIXTIME(start_time / 1000))注意:date将截断分钟,你只剩下日期