2012-01-31 68 views
0

我想發送郵件,當產品從過去和過時和過後的日期,在php中我用的是使用php的日期和mysql函數,但如果產品過期日期來像31-1 -2012,在不同的值不適合我的編碼,plz幫助如何解決它, 這是我的編碼PHP郵件發送時產品到期

<?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 = "[email protected]"; 
         $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 = "[email protected]"; 
         $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 = "[email protected]"; 
         $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']; 

       } 
      } 

      } 

     } 

} 
?> 
+0

數據庫的模式是什麼?另外,是否有一個原因,你沒有推遲到數據庫的處理? (畢竟,您可以修改第一個查詢以僅返回那些即將到期的行,以便您不必執行第二個查詢來執行日期比較) – 2012-01-31 08:50:32

+0

您的第二個選擇不需要。將一列添加到您現在將返回的第一條語句() - expired_on – alfasin 2012-01-31 08:56:12

+0

感謝您的回覆 – tamildinesh 2012-01-31 09:23:04

回答

0

嘗試這樣的事情...

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

-

SQL: WHERE $now >= DATE_FORMAT(expires, "%Y-%m-%d") 
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得到正確的差異值