用于读取数据库表中每一行日期的代码


Code to read each row of date in database table

目前,我开发了一个系统,可以在用户产品的保修期即将到期时通知用户。如果日期少于 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'];
}

我的发送邮件.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.";
    }
}   
?>
你可以

用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 ;

你应该提供更多详细信息。您的数据从何而来?无论如何,当您必须过滤数据时,您可以在不同的地方过滤它们:当您获取数据时(如果可能),或者在获取数据之后。

例如,如果数据来自数据库,您可以选择: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!";
    }
}

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

// $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!";
}