How to Get Last 3 Months Data in PHP MySQL?

Feb 18, 2022 . Admin

Hello Friends,

I am going to explain you example of how to get last 3 months data in PHP?. You will learn PHP query to get last 3 months data. I would like PHP select last 3 months. This article will give you simple example of PHP query to get last 3 months data.

We will use get search how to select last 3 months from news table using PHP. You can understand a concept of search how to take last 3 month dates from the current month in PHP. So, we can illustrate the getting a date 3 months in the past PHP.

You can see both example of how do I get my last 3 months data 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 
            * 
        FROM `users` 
        WHERE DATE(created_at) >= (DATE(NOW()) - INTERVAL 3 MONTH)";

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

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

?>
Output:
Array
(
    [id] => 2
    [name] => Aatman Infotech
    [email] => aatmaninfotech@gmail.com
    [created_at] => 2021-11-18 13:33:52.000000
    [updated_at] => 2020-09-30 13:33:52.000000
)
Array
(
    [id] => 3
    [name] => Haresh
    [email] => savanihd2@gmail.com
    [created_at] => 2021-12-15 06:46:08.000000
    [updated_at] => 2020-09-18 12:04:09.000000
)
It will help you...
#PHP