2013-12-13 83 views
0
<?php 
$con = mysql_connect("localhost","root",""); 
if(!$con) 
{ 
die("cannot connect" . mysql_error()); 
} 
mysql_select_db("test",$con); 
$sql = "SELECT Renewal_Date FROM ilist"; 
$mydata = mysql_query($sql,$con); 
$result_array = array(); 
echo "<table border=2 > 
<tr> 

    <th>Renewal_Date</th> 
</tr>"; 
    while($record = mysql_fetch_array($mydata)){ 
    echo"<tr>"; 

    echo "<td>" . $record['Renewal_Date'] . "</td>"; 
    echo "</tr>"; 


} 
echo "</table>"; 
    $date3=array(); 
    $todayDate = date('m/d/Y'); 
    $date1 = new DateTime($todayDate); 
    $date2 = new DateTime($record['Renewal_Date']); 
    foreach($date2 as $date3) 
     $interval = $date1->diff($date3); 
    echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d."  days "; 

mysql_close($con); 

?> 

我已經取續約日期列表到一個數組$紀錄,現在我需要使用的foreach到每個日期存儲在 一個變量,這個變量將是用於查找與當前日期的差異。 如何做到這一點?如何使用foreach循環到一個變量在PHP存儲陣列日期

regards Sanji 
+0

什麼錯誤?什麼不起作用? – Hussain

+0

格式不匹配錯誤在那裏。尚卡達摩達蘭的回覆工作正常。謝謝 – sanjisanjy

回答

0

重寫這樣的... [您不需要foreach結構,而不是做同樣的while循環本身裏面的東西]

while($record = mysql_fetch_array($mydata)){ 
    echo"<tr>"; 
    echo "<td>" . $record['Renewal_Date'] . "</td>"; 
    echo "</tr>"; 
    $todayDate = date('m/d/Y'); 
    $date1 = new DateTime($todayDate); 
    $date2 = new DateTime($record['Renewal_Date']); 
    $interval = $date1->diff($date2); // I assume renewal date should be greater , so it must be $date2->diff($date1); 
    echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d."  days "; 
} 
+1

Heyyi,謝謝..其工作。 :) – sanjisanjy

+0

關於續約日期,它應該大於今天的日期,否則我們必須增加罰款金額。這是一個圖書館系統。 – sanjisanjy

相關問題