gpt4 book ai didi

PHP检测输入的时间戳范围是否大于一天

转载 作者:行者123 更新时间:2023-11-30 22:19:53 24 4
gpt4 key购买 nike

我有一个日期选择器表单,可以选择两个范围之间的日期。当按下提交按钮时,它会调用一个查询来获取这两个日期之间的数据。我想要做的是,如果他们选择一个大于 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
);
};

最佳答案

这是你可以做的

SELECT Date, SUM(Count)
FROM testLocation
WHERE Date
BETWEEN '$data1%' AND '$data2%' GROUP BY date(Date)

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)

关于PHP检测输入的时间戳范围是否大于一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952517/

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