2013-03-25 55 views
-1

我想通過這個簡單的計算顯示病人的等待時間;使用PHP顯示等待時間

waiting time (the clock time - the arrival time) 

在插入表單中;我已經使用以下代碼將current_time(arrival_time)插入已成功的PATIENT表中。

// validate arrival time 
date_default_timezone_set('Europe/London'); 
$time = time(); 

$sql="INSERT INTO Patient(Forename, Surname, Gender, Date_Of_Birth, Address, Patient_History, Illness, Priority, Arrival_Time) 
VALUES('$patient_name', '$patient_lastname', '$gender', '$date', '$address', '$history', '$illness', '$priority', '$time')"; 

這裏是輸出代碼;

<?php 

$conn = mysqli_connect("localhost","root","") or die ("No connection"); 
mysqli_select_db($conn, "a&e") or die('Could not select database.'); 

$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_Time, TIMEDIFF(Arrival_time,NOW()) as Waiting_Time FROM Patient"; 
$result = mysqli_query($conn, $query) or die("Invalid query"); 

echo "<table border='1'> 
<tr> 
<th>PatientID</th> 
<th>Forename</th> 
<th>Surname</th> 
<th>Gender</th> 
<th>Illness</th> 
<th>Priority</th> 
<th>Waiting_Time</th> 
</tr>"; 

while ($row = $result->fetch_object()){ 


    echo "<tr> 
    <td>" . $row->PatientID . "</td> 
    <td>" . $row->Forename . "</td> 
    <td>" . $row->Surname . "</td> 
    <td>" . $row->Gender . "</td> 
    <td>" . $row->Illness . "</td> 
    <td>" . $row->Priority . "</td> 
    <td>" . $row->Waiting_Time . "</td> 
    </tr>"; 

} 

echo "</table>"; 
mysqli_close($conn); 
?> 

如何使用以下計算計算等待時間; (時鐘時間 - 到達時間)

**請注意:Arrival_Time類型爲TIME。

+1

您的查詢中沒有'TIMEDIFF(Arrival_time,NOW())as Waiting_Time'是否一樣? – cipher 2013-03-25 16:27:37

回答

0

你可以簡單地減去時間從當前時間的數據庫,然後使用date

$waiting_time = date("H:i:s", time() - $arrival_time); 
+0

虐待試試這個,謝謝 – user2192221 2013-03-25 16:31:37

0

我們通過這個已經在今天已經格式化。

Simple time function to calculate waiting time

你並不需要做的是在PHP作爲其在查詢$按行已經完成> Waiting_Time是時鐘時間與到達時間了。

如果您現在正在尋找一種基於PHP的解決方案而不是正確的解決方案,那麼您顯然正在嘗試做一些家庭作業,我不應該首先幫助您(更愚弄我),但不僅如此你可以告訴你的講師/老師他們是一個白癡,迫使你使用一個PHP解決方案,這個解決方案能夠更好地處理內置的MySQL函數。

+0

我知道我有它的工作,但是當我更新數據庫,到達時間功能wouldnt工作! – user2192221 2013-03-25 16:30:38

+0

這不是一個代碼問題,你發出輸入日期錯誤或列中的無效日期格式。 – Dave 2013-03-25 16:31:23

+0

數據庫中的arrival_time現在存儲爲TIME,而不是TIMESTAMP – user2192221 2013-03-25 16:32:32