2017-08-13 106 views
0

我想要實現的是按日期和時間排序(首先是pm,然後是pm)。當我用這段代碼嘗試它時(ORDER BY reservationdate DESC,reservationtime ASC),這就是我得到的。 enter image description here按時間排序am和pm

我對日期沒有問題,但時間沒有正確排序。我想要的是先用「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> 
+0

使用重複主題將日期和時間字符串轉換爲單個日期時間表達式,然後按該表達式進行排序。 – Shadow

回答

1

據推測,reservationtime存儲爲字符串不是時間。您應該使用適當的存儲方法。

可以使用str_to_date()方便拋時間:

order by str_to_date(reservationtime, '%l:%i %p') 

這將需要的AM/PM考慮。

+0

夥計。你從哪裏學到這些東西?我希望這是他們在學校所教的,而不是一些無用的課程。謝謝你,先生。 – Red

+1

@紅色。 。 。只要把它記錄下來,就可以看到老年人的經歷。 –