Простая функция времени для вычисления времени ожидания

Я пытаюсь получить простую функцию времени для работы с использованием PHP для вычисления времени ожидания пациента. Время прибытия вводится в таблицу пациентов как TIMESTAMP успешно; вот фрагмент кода для этого;

// validate arrival time $date_time=date('Ymd H:i:sa'); clock time date_default_timezone_set('Europe/London'); $the_time1 = date('G:ia'); 

время ожидания будет (часы – время прибытия)

Однако это не работает в моей части кода!

 <?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('Ymd 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); ?> , <?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('Ymd 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); ?> 

РЕДАКТИРОВАТЬ;

Теперь я изменил код;

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

Solutions Collecting From Web of "Простая функция времени для вычисления времени ожидания"

сделайте это в sql-запросе, так как у вас уже есть время прибытия в БД

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

Кроме того, в вашем примере выше вашего единственного когда-либо сравнивающего теперь время до настоящего времени, никогда не сравнивая с Arrival_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"); 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); ?> , <?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); ?>