Home > Blockchain >  How to loop specific data?
How to loop specific data?

Time:07-06

I want to create a admin panel and I want to display how many users visited the website in the past seven days. It's not efficient to loop every data in the database and then display some of them. I only want to loop the data from the past seven days.

I do have the user information as a D-M-Y format.

I did something like this but I don't think it's correct.

$TimeStamp       = time();
$OneWeek = 604800;
$FoundOneWeekAgo = $TimeStamp - $OneWeek;

$Query = $db->prepare('SELECT * FROM new_user');
$Query->execute();
$Count = $Query->rowCount();
$QueryRecords = $Query->fetchAll();


foreach ($QueryRecords as $Users) {
    
    $UserDate = $Users["Date"];
    $DataTimeStamp = strtotime($UserDate);

}

if ($DataTimeStamp >= $FoundOneWeekAgo) {
    //Don't know what to do here
}

Help me out I'm open to your thoughts about it.

Main idea : found the users visited at the specific date.

CodePudding user response:

Retrieve only specific data from the database.

Select * from new_user where date >= DATE(NOW() -INTERVAL 7 DAY)

If you have data in d-m-y format,you can do-

Select * from new_user where date >= DATE_FORMAT(DATE(NOW() -INTERVAL 7 DAY),"%d-%m-%y")
  • Related