【问题标题】:How to compare a date from mysql with current date and send a notification email如何将mysql中的日期与当前日期进行比较并发送通知电子邮件
【发布时间】:2009-09-08 11:42:28
【问题描述】:

我有具有日期列( mySQL 日期类型)的数据库条目。我想将该日期与当前服务器日期以及该日期的前 3 天(每个月)进行比较,以自动向指定地址发送电子邮件。

【问题讨论】:

  • 取决于编程语言和平台。

标签: mysql email date compare


【解决方案1】:

您还可以使用更简单的 MySQL TO_DAYS() 函数:

SELECT email FROM table WHERE TO_DAYS(date)=To_DAYS(NOW())-3;

【讨论】:

    【解决方案2】:
    Well this is just a mock but you could try something like this:
    
    $localTime = `date +"%Y/%m/%d %H:%M:%S"`
    
    // Query MySQL to get datetime
    
    // compare the two times and send email
    

    【讨论】:

      【解决方案3】:

      您可以让 PHP 或 MySQL 为您完成繁琐的工作,无论是脚本还是 SELECT 语句。

      <?php
      // We shall assume that you have a array called $send_date with values in the format YYYY-MM-DD (MySQL Date Format).
      // These values will be the values in the date column, and this script will send the email 3 days before those dates.
      
      $today = date("U");
      // Work out, in seconds, the amount of time beforehand you would like to send the email.
      $warning = 60 * 60 * 24 * 3;
      foreach($send_date as $timestamp)
       {
        $sd = strtotime($timestamp);
        // If $sd is not an integer, it is not a valid timestamp. Move onto the next.
        if(!is_int($timestamp)) continue;
        $diff = $timestamp - $today;
        if($diff > 0 && $diff <= $warning)
         {
          function_to_send_your_email($row_id_of_this_particular_date);
         }
       }
      

      或者一个 SELECT 语句:

      $warning = 60 * 60 * 24 * 3;
      $today = date("U");
      $sql = 'SELECT * FROM your_table WHERE TIMESTAMP(date_column) BETWEEN '.$today.' AND '.$today + $warning.';';
      // Perform query, and act on results brought back.
      

      这个脚本和一般的 PHP 唯一的问题是,它只会在脚本运行时提前检查三天(或$warning 中声明的值)。如果您希望它继续检查,我建议您设置一个 CRON 作业。 而且我不能保证其中任何一个都会起作用,因为我没有检查过它们。

      希望这能给你一些启发!

      【讨论】:

        猜你喜欢
        • 2016-01-03
        • 1970-01-01
        • 2017-10-03
        • 2013-02-23
        • 1970-01-01
        • 2017-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多