PHP MySQL Get Month Name from Date Example

Feb 22, 2022 . Admin

Hello Friends,

I am going to explain you example of PHP get month name from date example. You will learn MONTHNAME() Function in PHP. I would like Finding the Month name from given datetime Using MONTHNAME() Function when the date is NULL.

MONTHNAME() function in PHP is used to find month name from the given date. It Returns 0 when MONTH part for the date is 0 or greater than 12 otherwise it returns month name between January to December.

I will give you simple query to getting data get month name from date 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 
            *, 
            MONTHNAME(created_at) as month_name 
        FROM `users`";

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

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

?>
Output:
Array
(
    [id] => 1
    [name] => Divyesh Barad
    [email] => divyeshbarad@gmail.coim
    [created_at] => 2021-12-16 13:23:20.000000
    [updated_at] => 2022-02-21 13:23:20.000000
    [expired_at] => 2022-02-21 13:23:20.000000
    [month_name] => December
)
Array
(
    [id] => 2
    [name] => Aatman Infotech
    [email] => aatmaninfotech@gmail.com
    [created_at] => 2022-01-12 13:24:34.000000
    [updated_at] => 2022-01-12 13:24:34.000000
    [expired_at] => 2022-02-21 13:24:34.000000
    [month_name] => January
)
Array
(
    [id] => 3
    [name] => Haresh 
    [email] => savani@gmail.com
    [created_at] => 2022-02-21 13:25:28.000000
    [updated_at] => 2022-02-21 13:25:28.000000
    [expired_at] => 2022-02-21 13:25:28.000000
    [month_name] => February
)
It will help you...
#PHP