目前,我开发了一个系统,可以在产品保修期到期时通知用户。如果日期少于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.";
}
}
?>
答案 0 :(得分: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 ;
答案 1 :(得分: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!";
}
答案 2 :(得分: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!";
}
}
第一个解决方案性能更高,因为您无需获取并处理所有数据。