Envío de correo PHP cuando el producto expira

i want to send the mail , when product got expire from before and ondate and after day, in php i was used datediff mysql function with php, but if product expire date comes like 31-1-2012 , the differ value is not suit for my coding , plz help how to solve it , This is my coding

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

                }
             }

            }

        }

}
?>

preguntado el 31 de enero de 12 a las 08:01

What is the schema of the database? Also, is there a reason you're not deferring this processing to the database? (after all, you could modify the first query to only bring back the rows for those that are about to expire, so that you don't have to do a second query just to do a date comparison) -

your second select is not needed. add a column to your first select statement that will return now()-expired_on -

2 Respuestas

Write your second select query as follows

SELECT DATEDIFF($exdate, CURRENT_DATE) as diff;

Also, no need to write separate select query you can get same column in first query itself as

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

You may need to change position of expired_on and CURRENT_DATE to get correct diff value

Respondido el 31 de enero de 12 a las 15:01

Prueba algo como esto ...

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

-

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

Respondido el 31 de enero de 12 a las 12:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.