更改 PHP HTML 表单结果中的日期格式

Change Date Format in PHP HTML form results

我才刚刚开始学习 php 我已经尝试研究这个问题的答案,但找不到适合我情况的答案。请记住,我是菜鸟所以请详细解释。

显示结果的网页:https://fireytech.com/FireytechDatabase/hello.php

我有一个 php select 查询,它从我的 mysql 数据库中提取一个字段,其中保存了日期/时间戳。当我在 html 表单中获得查询的回显结果时,它以这种格式显示此日期字段:2022-01-31 00:00:00。我希望它显示为:01/31/2022(没有时间)。下面是我的表单代码。如何更改“DateReceived”和“DateLastEdited”字段的日期格式?

<?php

require '../../db_php/dbconnection.php';

$sql = "SELECT * FROM
            Tickets 
        Left JOIN
            Clients
        ON Tickets.CustomerNumber=Clients.CustomerNumber
        JOIN Contacts
        ON Contacts.ContactID=Clients.CustomerNumber
        JOIN Equipment
        ON Equipment.ContactID=Clients.CustomerNumber
        
        WHERE Equipment.PRINT = '-1' AND Contacts.DispatchContact = '-1' AND TicketStatus = 'Active' AND NOT TicketSubStatus = 'CUSTOMER PICKUP' AND NOT TaskSubType = 'ON-SITE'
";
$result = $conn->query($sql);

echo"<table border='1'>";
echo "<tr><td>Ticket Type</td><td>Ticket Number</td><td>Date Received</td><td>Last Edited</td><td>Last Name</td><td>Followup By</td><td>Sub Status</td><td>Repair Type</td><tr>";
if ($result->num_rows > 0){
while($row = $result->fetch_assoc() ){
    echo "<tr><td>{$row['TaskSubType']}</td><td>{$row['TicketNumber']}</td><td>{$row['DateReceived']}</td><td>{$row['DateLastEdited']}</td><td>{$row['ContactLast']}</td><td>{$row['FollowupBy']}</td><td>{$row['TicketSubStatus']}</td><td>{$row['ItemType']}</td></tr>";
}
} else {
    echo "0 records";
}

echo"</table>";

$conn->close();

?>

您可以在 sql 中使用 date_format(DateReceived,'%m/%d/%Y'),如果您想在 php 中更改日期格式,请使用函数 date('m/d/Y', strtotime($row['DateLastEdited' ]))

sql

中的示例
$sql = "SELECT date_format(DateReceived,'%m/%d/%Y') as DateReceived   FROM
            Tickets 
        Left JOIN
            Clients
        ON Tickets.CustomerNumber=Clients.CustomerNumber
        JOIN Contacts
        ON Contacts.ContactID=Clients.CustomerNumber
        JOIN Equipment
        ON Equipment.ContactID=Clients.CustomerNumber
        
        WHERE Equipment.PRINT = '-1' AND Contacts.DispatchContact = '-1' AND TicketStatus = 'Active' AND NOT TicketSubStatus = 'CUSTOMER PICKUP' AND NOT TaskSubType = 'ON-SITE'
";

php

中的示例
echo "<tr><td>{$row['TaskSubType']}</td><td>{$row['TicketNumber']}</td><td>{date('m/d/Y',strtotime($row['DateReceived']))}</td><td>{date('m/d/Y', strtotime($row['DateLastEdited']))}</td><td>{$row['ContactLast']}</td><td>{$row['FollowupBy']}</td><td>{$row['TicketSubStatus']}</td><td>{$row['ItemType']}</td></tr>";

要将日期转换为您想要的格式,只需输入

$dr= date("Y/d/m", strtotime($row['DateReceived']));
$dl= date("Y/d/m", strtotime($row['DateLastEdited']));
echo "<tr><td>{$row['TaskSubType']}</td><td>{$row['TicketNumber']}</td><td>{$dr}</td><td>{$dl}</td><td>{$row['ContactLast']}</td><td>{$row['FollowupBy']}</td><td>{$row['TicketSubStatus']}</td><td>{$row['ItemType']}</td></tr>";
echo date_format(date_create($date),"Y-m-d");