告诉我刚刚开始在php中编码, 我制作了这个程序,它从数据库中获取数据并以表格的形式打印出来。在我的数据库中,数据以hh:mm:ss格式存储,我希望以hh:mm方式检索数据。我该怎么办? bell_time是有时间的行,我的数据库的名称是bell_db。我用过phpmyadmin和mysql。我看到关于这个的其他帖子我无法理解它们。
// connect to the database
include('database.php');
// get results from database
$result = mysqli_query($conn, "SELECT * FROM bell_db")
or die(mysqli_error($conn));
// display data in table
echo "<table border='1' cellpadding='10' class='table table-hover'>";
echo "<thead><tr> <th>Bell No.</th> <th>No. of Rings</th> <th>Time</th> <th>Period</th> <th></th> <th></th></tr></thead>";
echo "<tbody>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['bell_no'] . '</td>';
echo '<td>' . $row['bell_amount'] . '</td>';
echo '<td>' . $row['bell_time'] . '</td>';
echo '<td>' . $row['bell_period'] . '</td>';
echo '<td><a href="edit.php?bell_no=' . $row['bell_no'] . '">Edit</a></td>';
echo '<td><a href="delete.php?bell_no=' . $row['bell_no'] . '">Delete</a></td>';
echo "</tr>";
}
// close table>
echo "</tbody>";
echo "</table>";
?>
答案 0 :(得分:2)
你可以用PHP来做,例如:
Session::now($key, $value);
在您的代码中:
<?php
$tim = "hh:mm:ss";
echo substr( $tim,0,5 ); // RETURNS "hh:mm".
?>
&#34; substr&#34;的语法是:
echo '<td>' . substr( $row['bell_time'],0,5 ) . '</td>';
答案 1 :(得分:1)
更改您的查询
SELECT * FROM bell_db
并获取必填字段
select bell_no,...,TIME_FORMAT(bell_time,'%H:%i').... FROM bell_db
答案 2 :(得分:0)
替换:
echo '<td>' . $row['bell_time'] . '</td>'
使用:
echo '<td>' . date('H:i', strtotime($row['bell_period'])) . '</td>';