如何仅显示当前一周的数据

时间:2019-05-05 12:48:00

标签: php mysql angular slim

我正在尝试从MYSQL数据库获取当前一周的数据,日期以毫秒为单位,并且我使用的是SLIM框架,当我显示数据时,我会得到结果,但会显示所有重复的日子。我的代码是

$app->get('/view/test/days', function ($request, $response, array $args) {
    try {
        $con = $this->db;
        // date_default_timezone_set('EAT');
        $sql = "SELECT start_time,COUNT(*) as tt FROM parking_sessions 
GROUP BY DAY(DATE (FROM_UNIXTIME(start_time/1000))),YEAR(DATE 
(FROM_UNIXTIME(start_time/1000)))";
        $result = null;
        $results = null;
        $tt = null;
        foreach ($con->query($sql) as $row) {

            $result[] = date('D',($row['start_time']/1000));
            $tt[] = $row['tt']; 
            $results['date'] =$result;

            $results['total'] =$tt; 
        }
        if ($result) {
            return $response->withJson(array('status' => 'true', 'date' => $results), 200);
        } else {
            return $response->withJson(array('status' => 'Owner Not Found'), 422);
        }

    } catch (\Exception $ex) {
        return $response->withJson(array('error' => $ex->getMessage()), 422);
    }
});

它给了我以下结果

{
"status": "true",
"date": {
    "date": [
        "Thu","Fri","Sun","Mon","Thu","Mon","Sat","Sun","Thu","Tue","Sun"
    ],
    "total": [
        "1","1","1","1","3","1","14","1","5","2","1"
    ]
}
}

所有需要的结果仅适用于当前周

    {
"status": "true",
"date": {
    "date": [
        "Sun","Thu","Tue","Sun"
    ],
    "total": [
        "1","5","2","1"
    ]
}
}

我的数据库就像

  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

0 个答案:

没有答案