How to Get Last 6 Months Data in PHP MySQL?

Feb 17, 2022 . Admin

Hello Friends,

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

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

You can see both example of how do I get my last 6 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 6 MONTH)";

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

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

?>
Output:
Array
(
    [id] => 2
    [name] => Aatman infotec
    [email] => aatmaninfo@gmail.com
    [created_at] => 2021-09-08 10:53:30
    [updated_at] => 2022-02-12 10:21:52
)
Array
(
    [id] => 3
    [name] => haresh
    [email] => hdsavani@gmail.com
    [created_at] => 2021-10-21 10:25:52
    [updated_at] => 2022-02-10 10:25:52
)
It will help you...
#PHP