1

目前,我开发了一个系统,当他们的产品保修即将到期时会通知用户。如果日期少于 45 天,则会自动向我发送一封电子邮件。现在,我使用下面的代码发送电子邮件。它可以毫无问题地运行。

$days = (strtotime("2013-9-23") - strtotime(date("Y-m-d"))) / (60 * 60 * 24);
if ($days<45)
    include 'sendmail.php';
else {
    echo "Problem!";
}

可能无法将日期逐一编码。所以,我认为系统可以读取表格本身中的每一行日期,但我不知道这样做。我该怎么办,这样上面的代码就可以读取数据而不需要具体的数据,比如“2013-9-23”等。

我的完整代码是:

// set database server access variables: 
$host = "localhost"; 
$user = "root"; 
$pass = ""; 
$db = "master_inventory";

// open connection 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

// select database 
mysql_select_db($db) or die ("Unable to select database!"); 


// create query 
$query = 'SELECT Lap_PC_Name, Lap_War_Expiry FROM laptop';
$date = 'SELECT Lap_War_Expiry FROM laptop';
// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) 

$days = (strtotime($date) - strtotime(date("Y-m-d"))) / (60 * 60 * 24);
if ($days<45)
    include 'sendmail.php';
else {
    echo "Problem!";
}

while($row = mysql_fetch_array($result)) {
    $Lap_PC_Name = $row['Lap_PC_Name'];
    $Lap_War_Expiry = $row['Lap_War_Expiry'];
}

我的 sendmail.php 代码:

<?php
// set database server access variables: 
$host = "localhost"; 
$user = "root"; 
$pass = ""; 
$db = "master_inventory";

// open connection 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

// select database 
mysql_select_db($db) or die ("Unable to select database!"); 

// create query 
$query = "SELECT * FROM laptop"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 10) {
    for ($query=1; $query<=$result; $query++){
        while($row = mysql_fetch_array($result)) {
            $Lap_PC_Name = $row['Lap_PC_Name'];
            $Lap_War_Expiry = $row['Lap_War_Expiry'];
        }
    }
    $to       = 'nazeni@domain.com';
    $subject  = 'Testing sendmail';
    $message  = 'The Following licensed will expired in less than one month. PC           
    Name:'.$Lap_PC_Name. '.The license will expire on '.$Lap_War_Expiry;
    $headers  = 'From: nazeni@domain.com';

    if (mail($to, $subject, $message, $headers)) {
        echo "Email sent.";
    }
    else {
        echo "Email sending failed.";
    }
}   
?>
4

3 回答 3

1

你可以用mysql来做这将是一个好方法

CREATE TABLE t (d1 timestamp, d2 timestamp);

INSERT INTO t VALUES ('2010-03-11 12:00:00', '2010-03-30 05:00:00');
INSERT INTO t VALUES ('2010-03-11 12:00:00', '2010-03-30 13:00:00');
INSERT INTO t VALUES ('2010-03-11 00:00:00', '2010-03-30 13:00:00');
INSERT INTO t VALUES ('2010-03-10 12:00:00', '2010-03-30 13:00:00');
INSERT INTO t VALUES ('2010-03-10 12:00:00', '2010-04-01 13:00:00');

SELECT d2, d1, DATEDIFF(d2, d1) AS diff FROM t;

+---------------------+---------------------+------+
| d2                  | d1                  | diff |
+---------------------+---------------------+------+
| 2010-03-30 05:00:00 | 2010-03-11 12:00:00 |   19 |
| 2010-03-30 13:00:00 | 2010-03-11 12:00:00 |   19 |
| 2010-03-30 13:00:00 | 2010-03-11 00:00:00 |   19 |
| 2010-03-30 13:00:00 | 2010-03-10 12:00:00 |   20 |
| 2010-04-01 13:00:00 | 2010-03-10 12:00:00 |   22 |
+---------------------+---------------------+------+
5 rows in set (0.00 sec)

或者您也可以添加查询

 SELECT d2, d1, DATEDIFF(d2, d1) AS diff FROM t where DATEDIFF(d2, d1) < 45 ;
于 2013-09-25T07:18:19.397 回答
0

你应该提供更多细节。你的数据来自哪里?无论如何,当你必须过滤你的数据时,你可以在不同的地方过滤它们:当你得到数据时(如果可能的话),或者在你得到数据之后。

例如,如果数据来自数据库,您可以选择: 1/ 使用 SQL 请求仅获取相关数据,然后针对这些情况发送邮件。

sample SQL code given by liyakat (see above)

2/ 从数据库中获取所有数据,并使用您的代码处理每一行,将硬编码的日期替换为数据库中的日期。

foreach($db_results as $row) {
    $days = (strtotime($row['date']) - strtotime(date("Y-m-d")))/ (60 * 60 * 24);
    if ($days < 45) {
        include 'sendmail.php';
    else {
        echo "Problem!";
    }
}

第一个解决方案性能更高,因为您不必获取和处理所有数据。

于 2013-09-25T07:35:22.023 回答
0
// $date is get from table
$days = (strtotime($date) - strtotime(date("Y-m-d")))/ (60 * 60 * 24);
if ($days < 45) {
    include 'sendmail.php';
else {
    echo "Problem!";
}
于 2013-09-25T07:20:02.213 回答