简单的时间函数来计算等待时间 [英] Simple time function to calculate waiting time

查看:120
本文介绍了简单的时间函数来计算等待时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个简单的时间函数来使用PHP来计算病人的等待时间。到达时间输入到患者表中作为TIMESTAMP成功;这里是这段代码;

I am trying to get a simple time function to work using PHP to calculate the waiting time of a patient. The Arrival time inputs into the patient table as a TIMESTAMP successfully; here is the piece of code for this;

// validate arrival time 

$date_time=date('Y-m-d H:i:sa');

clock time
date_default_timezone_set('Europe/London');
$the_time1 = date('G:ia');

等待时间将是(时钟时间 - 到达时间)

waiting time would be (the clock time - the arrival time)

但是,这不是我的代码中的工作!

However, this isn't working in my piece of code!

<?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 FROM Patient";
$result = mysqli_query($conn, $query) or die("Invalid query");

date_default_timezone_set('Europe/London');
$the_time1 = date('G:ia');
$date_time=date('Y-m-d H:i:sa');

echo Waiting_time($the_time1, $date_time); 

function waiting_time ( $the_time1, $date_time) {

$time_diff =  ( $the_time1, $date_time); {
$days      = floor( $time_diff / 86400 ); // 60 * 60 * 24 = number of seconds in a day
$time_diff -= $days * 86400;
$hours      = floor( $time_diff / 3600 ); // 60 * 60 = number of seconds in a hour
$time_diff -= $hours * 3600;
$mins       = floor( $time_diff / 60 ); // 60 = number of seconds in a minute

return( $days . ' days, ' . $hours . ' hours, ' . $mins . ' minutes' );

}


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>";

}
  echo "<tr>
  <td>" . $row[0] . "</td>
  <td>" . $row[1] . "</td>
  <td>" . $row[2] . "</td>
  <td>" . $row[3] . "</td>
  <td>" . $row[4] . "</td>
  <td>" . $row[5] . "</td>
  <td>" . $waitTime . "</td>
  </tr>";
  }
echo "</table>";
mysqli_close($conn);
?>

编辑;

我现在已将代码更改为此;

I have now changed the code to this;

<?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 as Waiting_Time FROM Patient";
$result = mysqli_query($conn, $query) or die("Invalid query");

date_default_timezone_set('Europe/London');

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);
?>

现在显示数据如下:

PatientID Forename Surname Gender Illness     Priority Waiting_Time 
249       Sara     Kearns  F      Immediate   high     2013-03-20 22:18:01

我需要等待时间在几个小时内显示1:15:23

I need the waiting time to display in hours e.g. 1:15:23

推荐答案

在sql查询中执行,因为您已经有到达时间在DB

do it in the sql query since you already have the arrival time in the DB

$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_Time, TIMEDIFF(Arrival_time,NOW()) as Waiting_Time FROM Patient";

另外在你上面的例子中,你只会比较现在的时间到现在的时间,从来没有真正的比较来自你的Arrival_Time DB

Also in your example above your only ever comparing now time to now time never actually comparing against Arrival_Time from your DB

代码修改版本

<?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");

date_default_timezone_set('Europe/London');

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[0] . "</td>
  <td>" . $row[1] . "</td>
  <td>" . $row[2] . "</td>
  <td>" . $row[3] . "</td>
  <td>" . $row[4] . "</td>
  <td>" . $row[5] . "</td>
  <td>" . $row[7] . "</td>
  </tr>";

}

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

这篇关于简单的时间函数来计算等待时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆