按时间顺序排列

时间:2017-08-13 15:19:07

标签: mysql mysqli

我想要实现的是按日期和时间订购表格(先是下午,然后是下午)。当我尝试使用此代码(ORDER BY reservationdate DESC,reservationtime ASC)时,这就是我得到的。 enter image description here

我对日期没有问题,但时间没有正确排序。我想要的是随时随地展示" am"首先是时间跟随" pm"。

这是我如何获取日期和时间并将其插入数据库

$reservationDate = date("F j, Y");
$reservationTime = date( g:i a"); 

$insertdata = "INSERT INTO reservations (reservationdate, reservationtime)VALUES('$reservationDate', '$reservationTime')";

这是显示它的代码

<?php
        $query = "SELECT * FROM reservations ORDER BY reservationdate DESC, reservationtime ASC LIMIT " . $this_page_first_result . ',' . $results_per_page;
        $search_result = filterTable($query);
?>

</div>
<!DOCTYPE html>
<html>
<head>
<title>Admin Control</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>

<form action="adminControl.php" method="GET">
<input id="searchBox" type="text" name="valueToSearch" placeholder="type information here">
<input id="searchButton" type="submit" name="search" value="Search">
<div style="overflow-x:auto;">
    <table class="reservations-table">
    <tr>
        <th class="thFirstName">First Name</th>
        <th class="thLastName">Last Name</th>
        <th class="thEmailAddress">Email Address</th>
        <th class="thContactNumber">Contact Number</th>
        <th class="thSpeaker">Speaker</th>
        <th class="thTopic">Topic</th>
        <th class="thLocation">Location</th>
        <th class="thAudience">Audience</th>
        <th class="thCount">Count</th>
        <th class="thTime">Time</th>
        <th class="thDate">Date</th>
        <th class="thAction">Reservation Date</th>
        <th class="thAction">Reservation Time</th>
        <th class="thAction">Status</th>
        <th class="thAction">Action</th>
        <th class="thAction">Action</th>
    </tr>
     <?php while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    <td><?php echo $row['firstname'];?></td>
                    <td><?php echo $row['lastname'];?></td>
                    <td><?php echo $row['emailaddress'];?></td>
                    <td><?php echo $row['contactnumber'];?></td>
                    <td><?php echo $row['speaker'];?></td>
                    <td><?php echo $row['topic'];?></td>
                    <td><?php echo $row['location'];?></td>
                    <td><?php echo $row['audience'];?></td>
                    <td><?php echo $row['count'];?></td>
                    <td><?php echo $row['time'];?></td>
                    <td><?php echo $row['date'];?></td>
                    <td><?php echo $row['reservationdate']?></td>
                    <td><?php echo $row['reservationtime']?></td>
                    <td><?php echo $row['reservationstatus'];?></td>
                    <td><?php echo "<a style='padding:6px; float:left; background-color:#45a049; color:white; border-radius: 3px;' href='adminControl.php?epr=approve&speakerName=".$row['speaker']."&reservationStatus=".$row['reservationstatus']."&reservationId=".$row['id']."'>approve </a>";?></td>
                    <td><?php echo "<a style='padding:6px; float:left; background-color:red; color:white; border-radius: 3px;' href='adminControl.php?epr=delete&reservationId=".$row['id']."'>delete</a>";?></td>
                </tr>
                <?php endwhile;?>
    </table>
    </form>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

据推测,reservationtime存储为字符串而不是时间。您应该使用适当的存储方法。

您可以使用str_to_date()

轻松投射
order by str_to_date(reservationtime, '%l:%i %p')

这将考虑am / pm。

相关问题