PHP MySQL Convert Date Format mm/dd/yyyy In Select Query Example


Share:
 | Published By: Admin | Published On: Feb 09, 2022 | Category: PHP

Hello Friends,

I am going to explain you example of PHP convert date format mm/dd/yyyy in select query example. You will learn convert date format yyyy/mm/dd to mm/dd/yyyy in PHP query. I would like to convert date mm/dd/yyyy in PHP. This article will give you simple example of convert date yyyy/mm/dd to mm/dd/yyyy in PHP server.

We will use change date format mm/dd/yyyy in PHP select statement. You can understand a how to convert date into mm/dd/yyyy format in PHP query.

Here i will give you many example convert date format dd/mm/yyyy to mm/dd/yyyy in PHP.

Table: user_payments

So, let's see bellow solution:

index.php
<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db_php";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT 
            *, DATE_FORMAT(payment_date,'%m/%d/%Y') as new_payment_date 
        FROM `user_payments`";

$result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {
    echo '<pre>';
    print_r($row);
}

?>
Output:

Array
(
    [id] => 1
    [user_id] => 1
    [charge] => 50
    [payment_date] => 2021-08-04
    [status] => 1
    [created_at] => 
    [update_at] => 
    [new_payment_date] => 08/04/2021
)
Array
(
    [id] => 2
    [user_id] => 2
    [charge] => 45
    [payment_date] => 2021-09-07
    [status] => 0
    [created_at] => 
    [update_at] => 
    [new_payment_date] => 09/07/2021
)
Array
(
    [id] => 3
    [user_id] => 3
    [charge] => 10
    [payment_date] => 2022-01-05
    [status] => 1
    [created_at] => 
    [update_at] => 
    [new_payment_date] => 01/05/2022
)
Array
(
    [id] => 4
    [user_id] => 4
    [charge] => 10
    [payment_date] => 2022-01-04
    [status] => 1
    [created_at] => 
    [update_at] => 
    [new_payment_date] => 01/04/2022
)
Array
(
    [id] => 5
    [user_id] => 5
    [charge] => 66
    [payment_date] => 2022-01-04
    [status] => 1
    [created_at] => 
    [update_at] => 
    [new_payment_date] => 01/04/2022
)
It will help you...
Share: