2010-11-27 66 views
0

我試圖通過時間戳發送自動電子郵件。我在DB中有一組unix時間戳。我拉他們然後嘗試並將它們相互比較。最後打破的部分。該foreach不返回正確的eblast id。我認爲我正確減去了unix時間戳,但我不確定。我希望if語句只在​​來自數據庫的時間戳是現在時間戳之前15分鐘時觸發。我只希望它在數據庫時間戳的15分鐘之前隨時啓動。如果它在數據庫的時間戳之後,則什麼也不做。希望這是有道理的。Array設置時間之前15分鐘找到一個

session_start(); 
date_default_timezone_set('America/Chicago'); 

error_reporting(E_ALL); 
include 'DB.php'; 

$timestamp = time(); 

echo 'Timestamp:'; 
echo $timestamp; 
echo '------'; 

$drop_dead = mysql_query("SELECT due_date, ID FROM eblasts") or die(mysql_error()); 

while ($row = mysql_fetch_array($drop_dead, MYSQL_NUM)) { 
     $final_email_id[$row[1]] = $row[0]; 
}; 

//this is the ID's of eblasts that need an email sent based on their drop_dead NOT on warning1, warning2 and due_date. 
print_r($final_email_id); 

foreach($final_email_id as $key => $value) { 
$query = mysql_query("SELECT warning_1 FROM eblasts WHERE ID = '$key'") or die(mysql_error()); 
$find_warning_1 = mysql_fetch_row($query); 
$warning_1[$key] = $find_warning_1[0]; 
} 
//this is the warning_1 numbers where the ID's of the array are the ID's of the eblast. 
print_r($warning_1); 

foreach($final_email_id as $key => $value) { 
$query = mysql_query("SELECT warning_2 FROM eblasts WHERE ID = '$key'") or die(mysql_error()); 
$find_warning_2 = mysql_fetch_row($query); 
$warning_2[$key] = $find_warning_2[0]; 
} 

//this is the warning_2 numbers where the ID's of the array are the ID's of the eblast. 
print_r($warning_2); 

foreach($final_email_id as $key => $value) { 
$query = mysql_query("SELECT due_date FROM eblasts WHERE ID = '$key'") or die(mysql_error()); 
$find_due_date = mysql_fetch_row($query); 
$due_date[$key] = $find_due_date[0]; 
} 

//this is the due date numbers where the ID's of the array are the ID's of the eblast. 
print_r($due_date); 

//Add 900 because its 15 minutes in seconds ... 
$run_time = $timestamp + 900; 

foreach($warning_1 as $key => $value){ 
$diff_time = $run_time - $warning_1[$key]; 
echo '-----'; 
echo $diff_time; 
echo '-----'; 
if($diff_time < 900 && $diff_time > 0){ 
echo 'different'; 
echo $diff_time; 
$_SESSION['eblast_id'] = $key; 
include 'warning_1.php'; 
} 
}; 

回答

1

假設warning_x字段是時間戳,太,這應該做的伎倆:

$timestamp = time(); 

$query = sprintf(
    'SELECT id, warning_1' . 
    ' FROM eblast' . 
    ' WHERE (UNIX_TIMESTAMP(warning_1) - %u) BETWEEN 1 AND 899', 
    $timestamp 
); 

while ($row = mysql_fetch_array($query, MYSQL_NUM)) { 
    $ids[$row[0]] = $row[1]; 
}; 

var_dump($ids); 
... 
+0

尼斯 - 但如果MySQL服務器運行數據庫服務器不在同一時區,我做了一個集時區在CST的腳本開頭......不是那樣搞砸了嗎? – 2010-11-27 01:20:45

0

你爲什麼不修改你的第一個SQL查詢,只選擇與時間戳列記錄小於當前時間的秒減去有15分鐘車值多少錢?