Select data that was added the last 7 days - php

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()

Related

Select column data from the last 7 days and echo results?

I am trying to pull amounts from the pay column of the loads table for the last 7 days and echo it to show results. i have tried several different way to do this and i am stumped.
<?PHP
$startDate = date("Y-m-d");
$endDate = strtotime(date("Y-m-d"). "-7 days");
$lsql ="SELECT * FROM loads WHERE created_at BETWEEN '$startDate 00:00:00' AND '$endDate 23:59:59'";
foreach ($link->query($lsql) as $ldata) {
echo $ldata['pay'];
}
?>
You probably just need to do:
SELECT * FROM loads WHERE created_at >= CURDATE() - INTERVAL 7 DAY;
directly from MySQL query. Or use DATE_SUB like:
SELECT * FROM loads WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
If CURDATE() is not working, change it to NOW() like:
SELECT * FROM loads WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)

Puling out data from the table from last week

$qry = "SELECT * FROM school WHERE WEEKOFYEAR(date) = WEEKOFYEAR(NOW()) ";
This query that i have currently in my code is getting me data from the current week starting from Monday. How can i get data from the table from last week or the week before. I have tried changing now to last week or currentweek.
any ideas is it possible to use weekofyear(()) and tweak it
You can do some date shift right in mysql, replace NOW() with
NOW() - INTERVAL 1 WEEK
there is also date_sub for subtracting
date_sub(NOW(),INTERVAL 1 WEEK)
Try this.
$qry = "SELECT * FROM school WHERE studId = $sessionId AND studentId = $student
AND date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY"

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()

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

Filter a range of dates with SQL

I have to filter the results of an SQL starting at the current day and displaying all records until the current day + 10 days. So if it's the 22th of December I should display all records starting that day and that aren't after the the 1st of January. How can I do this? I tried a simple query like the one below but it seems to only display the records until the last day of the current month and then instead of showing the records of the next year goes back to the first month of the current year.
SELECT *
FROM mytable
WHERE DAY(mydatefield) >= DAY(CURRENT_TIMESTAMP)
AND mydatefield <= DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 10 DAY)
EDIT 1
I'm using Symfony 2 + Doctrine 2 querybuilder so the query must be compatible with them
EDIT 2
I solved the Doctrine 2 problem by using the query suggested by eggyal with this PHP code
$queryBuilder->where($queryBuilder->expr()->gte('mydatefield', 'CURRENT_DATE()'))->andWhere($queryBuilder->expr()->lte('mydatefield', 'DATE_ADD(CURRENT_DATE(), 10, \'DAY\')'));
The problem lies in your erroneous use of MySQL's DAY() function, which returns the day of the month. You should use instead DATE(); you can also simplify with the BETWEEN ... AND ... comparison operator:
SELECT *
FROM mytable
WHERE DATE(mydatefield) BETWEEN CURDATE() AND CURDATE() + INTERVAL 10 DAY
Note that, in order to benefit from index optimisation, you could instead do:
SELECT *
FROM mytable
WHERE mydatefield >= CURDATE() AND mydatefield < CURDATE() + INTERVAL 11 DAY
You should be able to use this:
SELECT *
FROM mytable
WHERE date(mydatefield) >= date(CURRENT_TIMESTAMP)
AND date(mydatefield) <= date(DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 10 DAY))
See SQL Fiddle with Demo
try this
SELECT *
FROM mytable
WHERE DATE( mydatefield ) BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL 10 DAY

Categories