首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >时间数组查找比设置时间早15分钟的时间

问时间数组查找比设置时间早15分钟的时间
EN

Stack Overflow用户
提问于 2010-11-27 08:01:11
回答 2查看 340关注 0票数 0

我正在试着在时间戳的时候发送自动电子邮件。我在数据库中有一组unix时间戳。我把它们拉出来,然后试着把它们相互比较。在结尾处断开的部分。foreach没有返回正确的eblast id,我想我减去unix时间戳是正确的,但是我不确定。我希望if语句仅在数据库中的时间戳比now时间戳早15分钟时才触发。我只希望它发射时,如果它在15分钟之前的任何时间从数据库的时间戳。如果它在数据库的时间戳之后,那么什么也不做。希望这是有意义的。

代码语言:javascript
复制
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'; 
 }
};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-27 09:15:18

假设warning_x字段也是时间戳,这应该可以解决这个问题:

代码语言:javascript
复制
$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);
...
票数 1
EN

Stack Overflow用户

发布于 2010-11-27 08:55:00

为什么不修改第一个SQL查询,只选择时间戳列小于当前时间减去15分钟秒数的记录呢?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4289334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档