我有一個數據庫與幾個表與數據記錄與時間戳記作爲關鍵(dateGMT)。我正在嘗試創建一個返回1和0數組的函數,每天有4個小時或更長時間的數據存在0。查找數據差距從MySQL數據庫空間與PHP的日期
由於某種原因,我的功能將無法正常工作,我認爲這與搜索當天需要用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;
}
這是遺留應用程序的一部分嗎?請不要用'mysql_query'寫新代碼。這是從PHP中刪除的不推薦使用的界面。 – tadman
這是一個代碼,如上面的代碼以一種類似於smimilar的方式編寫的程序,我想當我有時間完成時,我將不得不完成整個過程,但是我應該使用什麼呢? – Gearbox
PDO似乎是現在最好的事情,[這並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-數據庫訪問/)。 – tadman