PHP MySQL Get First Day of Month From Date Example

Feb 19, 2022 . Admin

Hello Friends,

I am going to explain you example of PHP get first day of month from date example. You will learn how to first day of current month PHP. I would like first day of month PHP. This article will give you simple example of how to get first day of the month from date in PHP.

We will use get search PHP select first day of current month. You can understand a concept of search PHP get first day of month from date. So, we can illustrate the getting a PHP first day of month. We will instruct PHP select first day of month from data.

You can see both example of how do I get select first day of month PHP.

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 
            id,
            name,
            email,
            created_at, 
            DATE_ADD(LAST_DAY(created_at),INTERVAL 1 DAY) as first_date_of_month,
            DATE(LAST_DAY(created_at + INTERVAL 1 MONTH)) as last_date_of_month
        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.com
    [created_at] => 2021-12-16 10:13:07.000185
    [first_date_of_month] => 2022-01-01
    [last_date_of_month] => 2022-01-31
)
Array
(
    [id] => 2
    [name] => Aatman Infotech
    [email] => aatmaninfotech@gmail.com
    [created_at] => 2022-01-14 13:33:52.000000
    [first_date_of_month] => 2022-02-01
    [last_date_of_month] => 2022-02-28
)
Array
(
    [id] => 3
    [name] => Haresh
    [email] => savanihd2@gmail.com
    [created_at] => 2022-02-19 06:46:08.000000
    [first_date_of_month] => 2022-03-01
    [last_date_of_month] => 2022-03-31
)
It will help you...
#PHP