我有一个datepicker表单,用于选择两个范围之间的日期。按下提交按钮时,它会调用查询来获取这两个日期之间的数据。我想要做的是,如果他们选择一个超过24小时的日期范围,它将只显示每天的总数,而不是每天他们选择的每一天的每一个数据。我现在的表格有时间戳格式yyyy-dd-mm hh-mm-ss。如何使用PHP检测到这一点?
示例表
Date Count
2016-04-23 06:26:11 75
2016-04-23 07:30:12 90
2016-04-24 12:26:11 100
2016-04-24 06:27:14 200
2016-04-25 08:16:19 102
2016-04-26 13:10:07 300
示例代码
$link=Connection();
$data1 = '2016-04-23 06:26:11';
$data2 = '2016-04-23 06:30:11';
//if(isset($_POST['range1']) && $_POST['range2']){
//$data1 = $_POST['range1'];
//$data2 = $_POST['range2'];
$dteStart = new DateTime($data1);
$dteEnd = new DateTime($data2);
$dteDiff = $dteStart->diff($dteEnd);
echo $dteDiff->format('%a');
//Display one day of values
if($dteDiff == 0){
$result = mysql_query(
"
SELECT DISTINCT Date, Count
FROM testLocation
WHERE Date
BETWEEN '$data1%' AND '$data2%'
"
,$link
);
}
//Sum each day for the month
else if($dteDiff > 0 && $dteDiff < 30){
$result = mysql_query(
"
SELECT DISTINCT Date, Count
FROM testLocation
WHERE Date
BETWEEN '$data1%' AND '$data2%'
"
,$link
);
}
//Sum each month for the year
else if($dteDiff > 30){
$result = mysql_query(
"
SELECT DISTINCT Date, Count
FROM testLocation
WHERE Date
BETWEEN '$data1%' AND '$data2%'
"
,$link
);
};
答案 0 :(得分:0)
这是你可以做的事情
SELECT Date, SUM(Count)
FROM testLocation
WHERE Date
BETWEEN '$data1%' AND '$data2%' GROUP BY date(Date)
OR
SELECT Date, SUM(Count)
FROM testLocation
WHERE Date
BETWEEN '$data1%' AND '$data2%' GROUP BY DATE_FORMAT(Date, '%Y-%m-%d')
更新
SELECT DATE(Date) as dategroup, SUM(Count)
FROM testLocation
WHERE Date
BETWEEN '$data1%' AND '$data2%' GROUP BY DATE(Date)