How to Get Last 30 Days Data in PHP MySQL?

Feb 17, 2022 . Admin

Hello Friends,

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

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

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

$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] => 2022-01-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] => 2022-01-26 06:46:08.000000
    [updated_at] => 2020-09-18 12:04:09.000000
)
It will help you...
#PHP