How to Get Current Week Data in PHP MySQL?


Share:
 | Published By: Admin | Published On: Feb 15, 2022 | Category: PHP

Hello Friends,

I am going to explain you example of how to get current week data in PHP?. You will learn find current week data in PHP query. I would like to how to get data of current week only in PHP.

This article will give you simple example of find data of current week in PHP select statement. We will use get how to get data of current week only in PHP server.

We will use PHP query to get current week records.

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 WEEK(created_at) = WEEK(now())
        AND YEAR(created_at) = YEAR(now())";

$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] => 2022-02-14 10:21:52
    [updated_at] => 2022-02-12 10:21:52
)
Array
(
    [id] => 3
    [name] => haresh
    [email] => hdsavani@gmail.com
    [created_at] => 2022-02-15 10:25:52
    [updated_at] => 2022-02-10 10:25:52
)

It will help you...
Share: