php - 数据库中的 DATETIME 问题

标签 php mysql datetime date-arithmetic

我设计了以下代码;计算患者的等待时间和预期时间。如果患者等待时间过长,代码还应发出警告。

请注意: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);
?>

编辑: 在每一行的 Waiting Time 列中,它以秒为单位显示等待时间,括号中为 currentTime 减号和到达时间,仅用于检查。如何将等待时间转换为 hh:mm:ss 格式以更好地代表用户?

正在显示;

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

为什么显示 (10500.000000-10500.000000)?

最佳答案

试试这个:

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;

所以像这样:

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>

关于php - 数据库中的 DATETIME 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15658441/

相关文章:

php - 在 Phonegap 项目中组合 Ajax、Jquery、mysql、php 不起作用

php - 为网格布局拆分 PHP 数组

php - CakePHP - 从表中检索最常出现的值

php - 无法理解 PHP 中 DateTime 类的行为

Python Panda 数据框 - 从字符串到日期的整行

python - 按天过滤 Pandas 数据框

php - 将空数组元素删除到 json_encode 之后

Php Mysql,自定义 CMS 的文章标签

PHP 没有显示任何值,但肯定会传递值

MySQL 删除子查询需要数百秒,而选择查询成本要低得多