【发布时间】:2009-09-08 11:42:28
【问题描述】:
我有具有日期列( mySQL 日期类型)的数据库条目。我想将该日期与当前服务器日期以及该日期的前 3 天(每个月)进行比较,以自动向指定地址发送电子邮件。
【问题讨论】:
-
取决于编程语言和平台。
我有具有日期列( mySQL 日期类型)的数据库条目。我想将该日期与当前服务器日期以及该日期的前 3 天(每个月)进行比较,以自动向指定地址发送电子邮件。
【问题讨论】:
您还可以使用更简单的 MySQL TO_DAYS() 函数:
SELECT email FROM table WHERE TO_DAYS(date)=To_DAYS(NOW())-3;
【讨论】:
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
【讨论】:
您可以让 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 作业。
而且我不能保证其中任何一个都会起作用,因为我没有检查过它们。
希望这能给你一些启发!
【讨论】: