gpt4 book ai didi

php - 使用 php 从 mysql db 中查找数据间隙的日期

转载 作者:行者123 更新时间:2023-11-29 03:04:39 25 4
gpt4 key购买 nike

我有一个数据库,其中包含一些表,其中包含以时间戳为键 (dateGMT) 的数据记录。我正在尝试创建一个返回 1 和 0 数组的函数,其中 0 表示每天有 4 小时或更长时间的数据间隔。

由于某种原因,我的功能无法正常工作,我认为这与搜索我需要用 0 标记的天数有关。任何人都可以看出我在哪里可能犯了错误吗?

此外,由于某种原因,这感觉不太有效,也欢迎使用其他方法来解决原始任务!

提前致谢!

//create array with the dates from start of recordings to now
$period = iterator_to_array(new DatePeriod(new DateTime('2013-06-10'),new DateInterval('P1D'),new DateTime()));
$p2 = array();
$n = 0;
//the actual date is used as key for the number of the date
foreach($period as $p){
array_push($p2,date('Y-m-d',strtotime($p->format('Y-m-d'))));
//other way i tried: $p2[$p->format('Y-m-d')]= $n; $n++;
}

function makeArr($table,$p) {
$con = mysql_connect("127.0.0.1", "user", "pass");
mysql_select_db("table",$con);

$ret = array_pad(array(),count($p),0);
$query = "SELECT dateGMT FROM `$table` ORDER BY `dateGMT` ASC";
$result = mysql_query($query);

$d1 = strtotime('2013-06-10');
$n = 0;
while ($row = mysql_fetch_assoc($result, MYSQL_ASSOC)){
$d2 = strtotime(implode("",$row));
if($d1+14400 > $d2){
$ret[array_search(date('Y-m-d',$d1),$p)] = 1;
//part of the other way i tried: $ret[$p[$d1]] = 1;
}
$d1 = $d2;
}
return $ret;
}

最佳答案

有几种方法可以使用 sql 查询查找差距,查看这篇文章:Method of finding gaps in time series data in MySQL?

关于php - 使用 php 从 mysql db 中查找数据间隙的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17726849/

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