PHP MySQL Get Month Short Name From Date Example

Feb 23, 2022 . Admin

Hello Friends,

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

In PHP, you can use the DATE_FORMAT() function with the %b format specifier to return the short month name. For example, you can return Jan or Feb instead of January or February.You can use from PHP to display Month short name from date.

I will give you simple query to getting data get the short month 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, '%b') as month_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
    [month_name] => Jan
)
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
    [month_name] => Feb
)
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
    [month_name] => Feb
)
It will help you...
#PHP