我试图按日期对表格进行排序,但我已将其作为字符串输入。当它按日期列排序时,它只按日期的第一部分进行排序01/2016。如何让php代码查看整个字符串并进行排序?
这是我的代码:
<?php $usertype=0; ?>
<?php require('check_if_logged_in.php'); ?>
<?php
require_once 'header.php';
require_once('my_connect.php');
$my_query="select * from loans order by date_return ASC";
$result= mysqli_query($connection, $my_query);
?>
<html>
<h2 align="center">All Existing Loans</h2>
<body>
<p align="center">Here are all the loans that have been entered on the Loan System</p>
<p align="center"><font color="blue"><i> * Blue - Active</i></font></p>
<p align="center"><font color="red"><i> * Red - Overdue</i></font></p>
<p align="center"><font color="green"><i> * Green - Completed</i></font> </p>
</body>
</html>
<table border=1 cellpadding=10>
<tr><th>Loan ID<th>Product<th>User<th>Expected Return Date?<th>Returned? <th>Edit?
<?php
//$dbdate = date('Y-m-d', $date_return);
while ($myrow = mysqli_fetch_array($result)):
$today = date('d-m-Y');
$loanid = $myrow["loanid"];
$product = $myrow["product"];
$user = $myrow["user"];
$dbdate = date('d-m-Y',$myrow["date_return"]);
$date_return = $myrow["date_return"];
$returned = $myrow["returned"];
$table = '<tr>';
$table.= '<td>'.$loanid.'</td>';
$table.= '<td>'.$product.'</td>';
$table.= '<td>'.$user.'</td>';
if ($returned == 'Yes'){
$table.= '<td><div style="color: green;">'.$date_return.'</div>';
}else if ($date_return <= $today){
$table.= '<td><b><div style="color: red;">'.$date_return.'</div></b>';
} else {
$table.= '<td><div style="color: blue;">'.$date_return.'</div>';
} //end if
$table.= '<td>'.$returned.'</td>';;
$table.= "<td><a onClick ='"return confirm('Are You Sure You Want To Edit This Loan?')'" href=editloaninfo.php?loanid=".$loanid."><img src='"edit.png'"> </td>";
echo $table;
/*echo "
<tr>
<td>$loanid</td>
<td>$product</td>
<td>$user</td>
<td>$date_return</td>
<td>$returned</td>
<td><a onClick ='"return confirm('Are You Sure You Want To Edit This Loan?')'" href=editloaninfo.php?loanid=$loanid><img src='"edit.png'">";*/
endwhile;
?>
</table>
幸运的是,你的日期格式都是一样的,所以你可以试试这个:
$my_query="select * from loans order by STR_TO_DATE(date_return, '%m/%d/%Y') ASC";
将date_return的数据类型更改为date,但在此之前将所有字符串转换为日期形式的