产品过期时发送 PHP 邮件

标签 php mysql email

我想发送邮件,当产品从之前、日期和之后过期时,在 php 中,我在 php 中使用了 datediff mysql 函数,但如果产品过期日期类似于 31-1-2012 ,则不同值是不适合我的编码,请帮助如何解决它, 这是我的编码

<?php

$sql = mysql_query("SELECT * FROM supplier_product_det");
$results = mysql_num_rows($sql);
//echo '<script>alert("'.$results.'")</script>';
if ($results > 0)
{   

        for($i=0;$i<$results;$i++)
        {
            while($rows = mysql_fetch_array($sql))
            {
             /* print_r($rows['expired_on']);?><br /> <?*/
             $exdate = $rows['expired_on'];
             $curdate = date('Y-m-d'); 
             $datedif = mysql_query("select datediff('".$curdate."','".$exdate."')");
             while( $date = mysql_fetch_array($datedif) )
             {
                //echo $date['0'];
                //echo $rows['pro_code'];
                if (($date['0'])== 1)
                {

                    if(($rows['mailcount'])!== $curdate)
                    {
                        $to = $rows['supplier'];
                        $subject = "Product Expire Intimation";
                        $message = "Dear Mr/Miss/Mrs
                        The Following Your Product Expired 
                        Product Code:".$rows['pro_code']."
                        Product Name:".$rows['product_name']."
                        Product Expire Date:".$rows['expired_on']."";               
                        $from = "tone@bgrow.com";
                        $headers = "From:" . $from;
                        mail($to,$subject,$message,$headers);
                        $checkdate = mysql_query("update supplier_product_det set mailcount ='".$curdate."' where id='".$rows['id']."'") or die(mysql_error());
                    }


                    //echo $rows['supplier'];

                }
                    if (($date['0'])== 0)
                {
                    if(($rows['mailcount'])!== $curdate)
                    {
                        $to = $rows['supplier'];
                        $subject = "Product Expire Intimation";
                        $message = "Dear Mr/Miss/Mrs
                        The Following Your Product Expired
                        Product Code:".$rows['pro_code']."
                        Product Name:".$rows['product_name']."
                        Product Expire Date:".$rows['expired_on']."";
                        $from = "tone@bgrow.com";
                        $headers = "From:" . $from;
                        mail($to,$subject,$message,$headers);
                        $checkdate = mysql_query("update supplier_product_det set mailcount ='".$curdate."' where id='".$rows['id']."'") or die(mysql_error());
                    }


                    //echo $rows['supplier'];

                }
                    if (($date['0'])== -1)
                {
                    if(($rows['mailcount'])!== $curdate)
                    {
                        $to = $rows['supplier'];
                        $subject = "Product Expire Intimation";
                        $message = "Dear Mr/Miss/Mrs
                        The Following Your Product will Expire Today 
                        Product Code:".$rows['pro_code']."
                        Product Name:".$rows['product_name']."
                        Product Expire Date:".$rows['expired_on']."";
                        $from = "tone@bgrow.com";
                        $headers = "From:" . $from;
                        mail($to,$subject,$message,$headers);
                        $checkdate = mysql_query("update supplier_product_det set mailcount ='".$curdate."' where id='".$rows['id']."'") or die(mysql_error());
                    }


                    //echo $rows['supplier'];

                }
             }

            }

        }

}
?>

最佳答案

按如下方式编写第二个选择查询

SELECT DATEDIFF($exdate, CURRENT_DATE) as diff;

此外,无需编写单独的选择查询,您可以在第一个查询本身中获得相同的列

SELECT column1, column2, column3, DATEDIFF(expired_on, CURRENT_DATE) as diff
FROM supplier_product_det

您可能需要更改 expired_on 和 CURRENT_DATE 的位置才能获得正确的差异值

关于产品过期时发送 PHP 邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9076663/

相关文章:

ruby-on-rails - Sendgrid 上的 Heroku 错误

php - 对日期敏感的功能进行单元测试?

php - 如何使用 MySQL 创建今天、明天和昨天

java - 如何检测电子邮件地址是否真实

java - jdbc连接mysql时访问被拒绝

mysql - 如何一次为 2 列使用单个别名

email - 无法使用 gmail 对 SMTP 服务器错误进行身份验证

php - 将 PHP 时间转换为 PHP 中的 Javascript 时间

PHP cURL 未加载 Windows 8 64 位

mysql - MySQL 中 "almost sequential"主键的性能(InnoDB 索引)