How to Get Last 10 Days Data in PHP MySQL?

Feb 16, 2022 . Admin

Hello Friends,

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

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

You can see both example of how do I get my last 10 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 10 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-02-11 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-02-06 06:46:08.000000
    [updated_at] => 2020-09-18 12:04:09.000000
)
It will help you...
#PHP