2010-11-27 2 views
0

타임 스탬프로 자동 이메일을 보내려고합니다. DB에 유닉스 타임 스탬프 세트가 있습니다. 나는 그들을 끌어 와서 서로 비교한다. 끝 부분에서 부서지는 부분. foreach는 올바른 이블 ID를 반환하지 않습니다. 나는 유닉스 타임 스탬프를 정확하게 뺀 것으로 생각하지만 확신 할 수 없다. DB의 타임 스탬프가 지금 타임 스탬프보다 15 분 앞선 경우에만 if 문이 실행되기를 원합니다. DB에서 타임 스탬프가 15 분이되기 전에 언제든지 실행하기 만하면됩니다. DB에서 타임 스탬프가 지난 후 아무 것도하지 않습니다. 다행히도 그것은 의미가 있습니다.시간의 배열은 설정 시간 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 서버와 DB를 실행하는 서버는 같은 시간대에 있지 않은 경우와 나는 세트를했다 스크립트의 시작 부분에 CST에 표준 시간대를 넣으십시오. –

+0

예, DB의 구성에 따라 문제가 발생할 수 있습니다. 이를 반영하기 위해 내 게시물을 업데이트했습니다. – aefxx

+1

+1 - PHP가 아닌 db에서 수행하십시오. UTC 타임 스탬프로 작업하고 사용자에게 입출력을 할 때 표준 시간대를 변환하면 일상 생활이 쉬워집니다. –

0

타임 스탬프 열이 현재 시간 - 15 분 초보다 작은 레코드 만 선택하도록 첫 번째 SQL 쿼리를 수정하지 않는 이유는 무엇입니까?

관련 문제