产品到期时发送的PHP邮件

时间:2012-01-31 08:43:20

标签: php mysql email

我想发送邮件,当产品从之前和过期以及之后过期时,在php中我使用了带有php的日期功能,但如果产品过期日期如31-1-2012,则不同的值是不适合我的编码,PLZ帮助如何解决它, 这是我的编码

<?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'];

                }
             }

            }

        }

}
?>

2 个答案:

答案 0 :(得分:1)

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

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的位置以获得正确的diff值

答案 1 :(得分:0)

尝试这样的事情......

PHP: $now = date('Y-m-d', strtotime('now'));

-

SQL: WHERE $now >= DATE_FORMAT(expires, "%Y-%m-%d")