Find Last seven day record [duplicate] - php

This question already has answers here:
MySQL SELECT last few days?
(5 answers)
Mysql: results from last seven days
(2 answers)
Closed 8 years ago.
$today = date('Y-m-d');
$countreview= mysql_query("SELECT count(review),
time
FROM review
WHERE time BETWEEN ($today - INTERVAL 7 DAY) AND $today
GROUP BY time");

try this :
SELECT * from users where created_time > (NOW()-INTERVAL 7 DAY)

try
time BETWEEN DATE_SUB(DATE( '$today'),INTERVAL 7 DAY) AND '$today'
if you want compare with current time use now()
time BETWEEN DATE_SUB(DATE( NOW()),INTERVAL 7 DAY) AND NOW()

Try this
$sql="SELECT * FROM review WHERE DATE(time) = DATE_SUB( CURDATE( ) , INTERVAL 7)";
I hope this is what you are searching for
Happy Coding!!

You can use this in your MySQL WHERE clause to return records that were created within the last 7 days/week:
created >= DATE_SUB(CURDATE(),INTERVAL 7 day)
Also use NOW() in the subtraction to give hh:mm:ss resolution. So to return records created exactly (to the second) within the last 24hrs, you could do:
created >= DATE_SUB(NOW(),INTERVAL 1 day)

I think you will find it simplest :
$sevenDayOld = date('Y-m-d', strtotime('-7 days 00:00:00'));
$SQL = "SELECT count(review),time, FROM review
WHERE time > '" . $sevenDayOld ."'
GROUP BY time";

Related

How to pull week's worth of appointments with CURDATE + 7 Day INTERVAL

I have prepared a little pull request to pull appointments for Today from the DB:
$getAppointmentsToday = $db->prepare("SELECT * FROM appointments WHERE DATE(appointment_date) = CURDATE()");
$getAppointmentsToday->execute();
I tried modifying this statement to also pull appointments for the following 7 days, but running into some trouble:
$getAppointmentsWeek = $db->prepare("SELECT * FROM appointments WHERE DATE(appointment_date) = (CURDATE(), INTERVAL 7 DAYS)");
$getAppointmentsWeek->execute();
You can use the BETWEEN operator for this :
...
WHERE DATE(appointment_date)
BETWEEN CURDATE()
AND CURDATE() + INTERVAL 7 DAY
You are now checking the date CURDATE()+7 days, instead of the date interval. Use:
SELECT * FROM appointments WHERE DATE(appointment_date) <= (CURDATE(), INTERVAL 7 DAYS) AND DATE(appointment_date) >= CURDATE()

how to get the records from a table of last 7 days [duplicate]

This question already has answers here:
Display results from past 7 days PHP
(3 answers)
Closed 8 years ago.
if(isset($_POST['last']))
{
$s=mysql_query("select * from appointment where `date` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)");
echo $s;
while($last=mysql_fetch_array($s))
{
echo $last['date'];
echo $last['doctor'];
}
}
how to get the records of last 7 days from a table after clicking of button, the field name of date is "date"
Your Query should look like below------
SELECT * FROM appointment WHERE `date`>= DATEADD(day, -7, GetDate())
SELECT * FROM `appointment` WHERE `date`>= DATE_SUB(CURDATE(),INTERVAL 7 DAY)
Use above query for your solution.
Try This buddy!
WHERE date <= NOW() AND date >= DATE_SUB(date, INTERVAL 7 DAY)

get records of today, yesterday and this week [duplicate]

This question already has an answer here:
From the timestamp in SQL, selecting records from today, yesterday, this week, this month and between two dates php mysql
(1 answer)
Closed 8 years ago.
I want to show records of today (from yesterday 12 AM to today 11:59 PM), Yesterday, and records of this week,
I have this query for todays records
SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= NOW() AND `TimeStamp` >= ?????
I have a field in my table named TimeStamp format is 2014-09-20 12:11:20
Make your calculations based on CURDATE if you are selecting date only.
Hope the below Examples would help you
Today: WHERE timestamp >= CURDATE()
Yesterday: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND timestamp < CURDATE()
This month: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY)
Between the two dates 3 June 2013 and 7 June 2013 (note how the end date is specified as 8 June, not 7 June): WHERE timestamp >= '2013-06-03' AND timestamp < '2013-06-08'
Please see this one too..
Use strtotime() and date() function for getting yesterday. Example....
$now = date('Y-m-d h:i:s', time());
$yesterday = date('Y-m-d h:i:s', strtotime('yesterday'));
$sql = "SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= '$now' AND `TimeStamp` >= '$yesterday'";
Also can use BETWEEN in query String. ie,.
$sql = "SELECT * FROM tblpatients WHERE Is_Deleted = '0' AND TimeStamp BETWEEN '$now' AND '$yesterday'";

Select data that was added the last 7 days

I have a table where the time is a date type. I would like to select all the records that were added the last 7 days and then out put them in an xml file. I can select all data and output it fine without a WHERE statement.
Here is the code:
$query_feed = "SELECT * FROM keysound_data WHERE time >=DATE_SUB(CURDATE(), INTERVAL 7 DAY AND time <= CURDATE()";
$feed = mysql_query($query_feed, $dconn) or die(mysql_error());
$row_feed = mysql_fetch_assoc($feed);
$totalRows_feed = mysql_num_rows($feed);
echo'<items>';
while ($row_feed = mysql_fetch_assoc($feed)){
echo'
<item>
<name>'.$row_feed['Name'].'</name>
<email>'.$row_feed['email'].'</email>
<date>'.$row_feed['Date'].'</date>
<description>'.$row_feed['Make'].' '.$row_feed['Model'].' '.$row_feed['Type'].'</description>
<logon>'.$row_feed['Logon'].'</logon>
<category>'.$row_feed['Type'].'/'.$row_feed['Make'].'</category>
<product_search_code>'.$row_feed['Product_search_code'].'</product_search_code>
<order_ref>'.$row_feed['Invoice'].'</order_ref>
<product_link>'.$row_feed['Product_link'].'</product_link>
<customer_ref>'.$row_feed['Invoice'].'</customer_ref>
<amount>'.$row_feed['Price'].'</amount>
<currency>GBP</currency>
</item>';
}
echo '</items>';
Not sure what's going wrong. Any help welcome
You're missing a bracket in your SQL to close DATE_SUB function.
Try this:
SELECT *
FROM keysound_data
WHERE
time >=DATE_SUB(CURDATE(), INTERVAL 7 DAY)
AND time <= CURDATE()
Better yet, you could use BETWEEEN to optmize your query:
SELECT *
FROM keysound_data
WHERE
time BETWEEN (CURDATE() - INTERVAL 7 DAY) AND CURDATE()
EDIT: As #spencer7593 noticed, CURDATE() - INTERVAL 7 DAY should be used instead of DATE_SUB(CURDATE(), INTERVAL 7 DAY) for even better optimization. The query above was updated to make use of that.
You are missing a closing bracket.
Try this-
time >=DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND time <= CURDATE()
SELECT *
FROM keysound_data
WHERE
time BETWEEN (now() - INTERVAL 7 DAY) AND now()

Get data from database inserted in latest 7 days

I am in problem in mysql database data retrieving. I have to get latest data inserted within a week or latest 7 days. I just know get data of specific date, but no within a span of days.
Please anyone help me. I am new in mysql.
You are looking for INTERVAL. For example, this will find all users whose created_time is in last 7 days and you have field created_time to tracked date of creation of record
SELECT * from users where created_time > (NOW()-INTERVAL 7 DAY)
You can do it using below query.
SELECT *
FROM `table`
WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND
CURDATE()
OR
SELECT * FROM `table` WHERE `date` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
OR
SELECT * FROM table WHERE DATEDIFF(NOW(),dateField) < 7
we can use DATE_SUB Mysql default function
select * from yourtable where date_of_insertion >= DATE_SUB(NOW(),INTERVAL 7 DAY)
<?php
$date = date("your_date_format", strtotime(- 7 days));
$result = mysqli_query($link, "SELECT * FROM table WHERE `date` > '$date'");
?>
Simple as that.
try :
select * from tablename where add_time >= now() - interval 7 day

Categories