2013-03-27 52 views
1

我设计了下面的代码;计算患者的等待时间和预计时间。如果患者等待时间过长,该代码还应回应警告。DATETIME在数据库中的问题

请注意:Waiting_time是数据库中的DATETIME。

这是代码;

<?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,NOW() as now,ABS(TIMEDIFF(NOW(), Arrival_time)) 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()){ 

//Select the expected and discharge time for this patient. 
    $query2 = "SELECT Abs(TIMEDIFF(Expected_Time,'00:00:00')) as Expected,Abs(TIMEDIFF(Discharge_Time,'00:00:00')) as Discharge ". 
       "FROM priority_time ". 
       "WHERE Illness = '".$row->Illness."'". 
        " AND Priority = '".$row->Priority."'". 
       ";"; 

    $result2 = mysqli_query($conn, $query2) or die("Invalid statement: ".$query2); 

    $row2 = $result2->fetch_object(); 
    $expected = $row2->Expected; 

    $discharge = $row2->Discharge; 
    echo "expected-> ".$expected." discharge-> ".$discharge; 

    if($expected > $discharge){ 
     echo "There is a problem with the database consistency, expectedTime must be less than dischargeTime!"; 
    } 
    //Set the patient color. 
    if($row->Waiting_Time < $expected && $row->Waiting_Time < $discharge){ 
     echo "<tr>"; 
    } 
    if($row->Waiting_Time >= $expected && $row->Waiting_Time < $discharge){ 
     echo '<tr bgcolor="#FFFF00">'; 
    } 
    if($row->Waiting_Time > $expected && $row->Waiting_Time > $discharge){ 
     echo '<tr bgcolor="#FF0000">'; 
    } 

    //Print patient info 
    echo 
     "<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 . "(".$expected."-".$discharge.") </td>"; 

    //Close row 
    echo "</tr>"; 

} 

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

编辑: 在每一行,列等待时间则显示秒的等待时间,并在括号中的currentTime的负和到达时间,只是为了检查。如何将等待时间转换为hh:mm:ss格式以便为用户提供更好的表示形式?

显示;

Waiting time 
01:15:42(10500.000000-10500.000000) 

为什么显示(10500.000000-10500.000000)?

+0

问题在哪里? – LuigiEdlCarno 2013-03-27 12:11:53

+0

输出是问题,其顶部显示预期 - > 10500.000000放电 - > 10500.000000顶部的表格。 – 2013-03-27 12:12:56

+0

好的,sry。想想,你刚刚发布的那样,作为比较 – LuigiEdlCarno 2013-03-27 12:15:15

回答

0

试试这个:

gmdate("H:i:s", $seconds) 

如果不行,看看How to convert seconds to time format?

更新:

为此在SQL语句尝试是这样的:

SELECT TIME_TO_SEC(TIMEDIFF('2013-03-27 12:00:00', '2013-03-27 10:00:00')) diff; 

S Ø是这样的:

SELECT PatientID 
, Forename 
, Surname 
, Gender 
, Illness 
, Priority 
, Arrival_time 
, NOW() as now 
, TIME_TO_SEC(TIMEDIFF(NOW(), Arrival_time)) as Waiting_Time 
FROM Patient; 

那么你会改变这一行:

<td>" . $row->Waiting_Time . "(".$expected."-".$discharge.") </td> 

这样:

<td>" . gmdate("H:i:s", $row->Waiting_Time) . "(".$expected."-".$discharge.") </td> 
+0

我把这个添加到SQL语句中以将秒转换为h:i:s? – 2013-03-27 12:28:54

+0

在我的文章的更新中添加了您可以在SQL语句中执行的操作。 – llanato 2013-03-27 12:33:02

+0

编辑问题以显示此处显示的内容; waiting_time as; 3382(10500.000000-10500.000000) – 2013-03-27 12:41:27

0

当日期和时间在PHP的strtotime处理()是你最大的朋友。幸运的是date()函数就是你所需要的。将秒转换为可读格式的内容可能如下所示。

$time = strtotime('now'); 
echo $time . '<br />'; 
$var = date('H:i:s',$time); 
echo $var; 
+0

在哪里我会把这个代码转换? – 2013-03-27 12:33:20

+0

我会把它放在你的数据库对象提取下面。每次需要时都要这样做,这样你就可以将这些时间与strtotime($ convertedTime)> strtotime($ otherConvertedTime)进行比较,然后在回声的底部可以使用这些变量。 – Scottzozer 2013-03-27 17:02:58

+0

如果你的着色效果很好,你对它的期望如何,我实际上只是在回声之前就这样做,你不会必须改变很多 – Scottzozer 2013-03-27 17:04:04