我有以下代码,它将sql数据填充到html表中。当我尝试格式化"order_date"的日期时,不知何故,我无法让它工作。这里有人知道如何格式化日期以月日、年(例如 20015 年 5 月 3 日)格式显示吗?我使用的语法 date("M-d-y", strtotime($row['order_date']))似乎不起作用。我在下面拥有的代码仅适用于填充数据,但不适用于我想要的order_date的日期格式。另外,如果我想先填充 sql 中的最后一项,如何更改代码?我现在拥有的代码从第一行填充数据到最后一行,但我想将填充顺序从最后一行数据更改为第一行。有什么帮助吗?
$sql = "从id
0中选择CC_1、contract_number
、property_name
、property_address
、city
、state
、zip
、status
、order_date
"; $result = $DB_CON->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
$data_row = '<table class="table table-striped tablesorter">'
. '<thead>'
. '<tr>'
. '<th>ID</th>'
. '<th>Contract Number</th>'
. '<th>Property Name</th>'
. '<th>Street Address</th>'
. '<th>City</th>'
. '<th>State</th>'
. '<th>Zip</th>'
. '<th>Date</th>'
. '<th>Status</th>'
. '</tr>'
. '<tbody>';
foreach($result as $row) {
$data_row .= '<tr>'
. '<td scope="row" class="id-c text-center">'.$row['id'].'</td>'
. '<td>' .$row['contract_number'].'</td>'
. '<td>' .$row['property_name'].'</td>'
. '<td>' .$row['property_address'].'</td>'
. '<td>' .$row['city'].'</td>'
. '<td>' .$row['state'].'</td>'
. '<td>' .$row['zip'].'</td>'
. '<td>' .date("M-d-y", strtotime($row['order_date'])).'</td>'
. '<td>' .$row['status'].'</td>';
}
}
$data_row .= '</tbody>'
. '</table>';
echo $data_row;
首先,您可以更新查询以按订单日期降序对结果进行排序。因此,最新的订单将首先出现。例如:
$sql = "SELECT id, contract_number, property_name, property_address, city, state, zip, status, order_date FROM ".$email." ORDER BY order_date DESC"; $result = $DB_CON->query($sql);
其次,正如 RepeaterCreeper 在评论中建议的那样,您可以更改此行:
. '<td>' .date("M-d-y", strtotime($row['order_date'])).'</td>'
对此:
. '<td>' .date("F d, Y", strtotime($row['order_date'])).'</td>'