MySQL PHP Get Last Day Of Month From Date Example

Feb 21, 2022 . Admin

Hello Friends,

I am going to explain you example of PHP get last day of month from date example. You will learn get last day of month from date in PHP query. I would like find record last day of month from date in PHP. This article will give you simple example of get last day of month from date record in PHP.

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

We will use get search how to find the last day of the month for a given date in 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] => nikhil thumar
    [email] => nik123@gmail.com
    [created_at] => 2022-01-08 09:28:17
    [first_date_of_month] => 2022-02-01
    [last_date_of_month] => 2022-02-28
)
Array
(
    [id] => 2
    [name] => hardik savani
    [email] => hardi123@gmail.com
    [created_at] => 2022-02-03 09:31:32
    [first_date_of_month] => 2022-03-01
    [last_date_of_month] => 2022-03-31
)
Array
(
    [id] => 3
    [name] => aatman infotec
    [email] => aatmaninfotec@gmail.com
    [created_at] => 2022-02-19 09:33:47
    [first_date_of_month] => 2022-03-01
    [last_date_of_month] => 2022-03-31
)
It will help you...
#PHP