douxiexie3574 2019-06-03 15:31
浏览 86
已采纳

如何在X时间后仅发送一次电子邮件

I am making a PHP alarm notification that will send an email if a temperature set point has been reached.

I have CRONJOB that runs in every 1 minute, runs a PHP script that verifies the temperature and if the temperature reaches to a point, it will send an email notification to me.

The problem is, that it will send me a email notification every minute while the temperature is passed the set point due to the CRONJOB. I would like for it to only send me an email once. Not send one minute.

I am using MYSQL to store the alarm information. If I need to some how setup time stamp in mysql and have the script verify the time and send it during x amount of time.?

Thanks,

  • 写回答

1条回答 默认 最新

  • duangu6431 2019-06-03 15:56
    关注

    You could either do the calculation in your application code or directly in the DB. Here's some pseudo code.

    Application Code

    CONST NOTIFICATION_DURATION = '30 minutes';
    
    $mostRecentNotification = query('SELECT MAX(created_at) FROM notifications');
    
    // Notice we're doing the calculation in our application code.
    $nextAvailableNotification = $mostRecentNotification + NOTIFICATION_DURATION;
    
    if (current_time >= $nextAvailableNotification) {
        send_notification();
    }
    

    If you choose to go with the application code version, I highly recommend using a library like Carbon (https://carbon.nesbot.com/) to help you out with your date/time arithmetic. It truly is a life saver even with simple calculations like this one.

    Database Version

    CONST NOTIFICATION_DURATION = '30 minutes';
    
    // Notice we're doing our calculation in our database
    $nextAvailableNotification = query('SELECT MAX(created_at) + INTERVAL ' . NOTIFICATION_DURATION . ' FROM notifications');
    
    if (current_time >= $nextAvailableNotification) {
        send_notification();
    }
    

    You could further compress the database version by doing the comparison directly in your query but I'll leave that up to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度