I have a db with a few tables with data recordings with timestamps as key (dateGMT). I am trying to create a function that returns an array of 1 and 0, with 0 for every day where there is a gap in the data of 4 hours or more.
For some reason my function won't work, I think it is related to when searching for the number of the day i need to mark with 0. Anyone able to see where i might have made my mistake?
Also, this does not feel too efficient for some reason, other ways to solve the original task are also welcome!
Thanks in advance!
//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;
}