How to Select Data Between Two Dates in PHP MySQL?

Feb 21, 2022 . Admin

Hello Friends,

I am going to explain you example of how to select data between two dates in PHP?. You will learn select data between two dates in PHP. I would like find data between two dates in PHP. This article will give you simple example of get record between two dates in PHP query.

Now, let's see article of PHP query select data between two dates in PHP Code Example. We will use this example of get record for two date data.

I will give you simple query to getting data between two dates from table.

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 created_at >= '2022-02-01' 
		AND created_at <= '2022-02-31'";

$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-17 13:24:34.000000
    [updated_at] => 2022-02-21 13:24:34.000000
    [expired_at] => 2022-02-21 13:24:34.000000
)
Array
(
    [id] => 3
    [name] => Haresh 
    [email] => savani@gmail.com
    [created_at] => 2022-02-21 13:25:28.000000
    [updated_at] => 2022-02-21 13:25:28.000000
    [expired_at] => 2022-02-21 13:25:28.000000
)
It will help you...
#PHP