Home > OS >  Change Date Format in PHP HTML form results
Change Date Format in PHP HTML form results

Time:02-04

I'm just starting to learn php and I've tried to research an answer to this but I can't find an answer that fits my situation. Remember, I'm a noob so please explain in detail.

Webpage where results are shown: https://fireytech.com/FireytechDatabase/hello.php

I have a php select query that pulls a field from my mysql database where a date / timestamp is saved. When I get the echo results of the query in my html form, it displays this date field in this format: 2022-01-31 00:00:00. I would like it to show as: 01/31/2022 (no time). Below is my form's code. How would I change date format for my "DateReceived" and "DateLastEdited" fields?

<?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();

?>

CodePudding user response:

you can use date_format(DateReceived,'%m/%d/%Y') in sql, if you want to change date format in php use function date('m/d/Y', strtotime($row['DateLastEdited' ]))

Example in 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'
";

Example in 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>";

CodePudding user response:

To transform the dates to the format you want, simply put

$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>";
  •  Tags:  
  • Related