PHP MySQL Get Day Short Name from Date Example

Feb 24, 2022 . Admin

Hello Friends,

I am going to explain you example of PHP get day short name from date example. You will learn How to get the short day name from a date in PHP?. DATE_FORMAT '%a' for short day name in PHP query.

In PHP, you can use the DATE_FORMAT() function with the %a format specifier to return the short day name. For example, you can return Sun or Mon instead of Sunday or Monday.

You can use from PHP to display day short name from date. I will give you simple query to getting data get the short day name from table.

Table: users


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(created_at, '%a') as day_name 
        FROM `users`";

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

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

?>
Output:
Array
(
    [id] => 1
    [name] => nikhil thumar
    [email] => nik123@gmail.com
    [created_at] => 2022-01-08 09:28:17
    [updated_at] => 2020-02-01 09:28:17
    [expired_at] => 2022-03-17 09:28:17
    [day_name] => Sat
)
Array
(
    [id] => 2
    [name] => hardik savani
    [email] => hardi123@gmail.com
    [created_at] => 2022-02-03 09:31:32
    [updated_at] => 2020-03-09 09:31:32
    [expired_at] => 2022-01-31 09:28:17
    [day_name] => Thu
)
Array
(
    [id] => 3
    [name] => aatman infotec
    [email] => aatmaninfotec@gmail.com
    [created_at] => 2022-02-19 09:33:47
    [updated_at] => 2020-03-19 09:33:47
    [expired_at] => 2022-01-31 09:28:17
    [day_name] => Sat
)
It will help you...
#PHP