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)
Related
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()
I want to get all the records from mysql database who have updated their records within 30 days from the current date for that i have used the below query but it is not working properly. $tda is the current date and $prevmonth is the date of exactly
30 days back from the current date. Please help. Thanks.
$da=date('d');
$tda=date('d-m-Y');
$prevmonth = date(''.$da.'-m-Y', strtotime('-1 months'));
$sql_q=executeQuery("select * from ".reg." where 'uid' !=".$_SESSION['uid']." AND Updatedate >= '$prevmonth' AND Updatedate <='$tda '");
You can do it in mysql as
`Updatedate` < DATE(NOW() - INTERVAL 30 DAY)
OR
`Updatedate` < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
For Varchar
STR_TO_DATE(Updatedate, '%Y-%m-%d') < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
UPDATE :
The query posted in the comment is wrong and should be
$sql_q=executeQuery("select * from registration
where
`uid` != ".$_SESSION['uid']."
AND STR_TO_DATE(Update_date, '%d-%m-%Y') < DATE_SUB(CURDATE(), INTERVAL 30 DAY)") ;
If you are looking for data within last 30 days then
$sql_q=executeQuery("select * from registration
where
`uid` != ".$_SESSION['uid']."
AND STR_TO_DATE(Update_date, '%d-%m-%Y') >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)") ;
I like doing something like this: AND UpdateDate > NOW() - INTERVAL 30 DAY AND UpdateDate < NOW().
If your Updatedate column is a DATETIME column then you can do the following:
SELECT *
FROM table
WHERE uid <> ?
AND Updatedate >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND Updatedate <= NOW();
Or:
SELECT *
FROM table
WHERE uid <> ?
AND Updatedate BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW();
Or if it's a timestamp then this:
SELECT *
FROM table
WHERE uid <> ?
AND FROM_UNIXTIME(Updatedate) >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND FROM_UNIXTIME(Updatedate) <= NOW();
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()
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
Here's my query:
SELECT *
FROM daily_records
AND date = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
I use this to generate a report of everything that happened yesterday. This works great, Tuesday-Friday. However, on Mondays, I want it to be able to search back to Friday (the previous business day, eg INTERVAL 3 DAY).
Is there a way to do this in mySQL? Or do I just need to check the day of the week in PHP before writing the query?
This should do it:
SELECT * FROM daily_records AND date =
DATE_SUB(CURDATE(), INTERVAL IF(DATE_FORMAT(NOW(), '%w') = 1, 3, 1) DAY)
You could do...
SELECT *
FROM daily_records
WHERE date = IF(DAYOFWEEK(CURDATE()) = 2, DATE_SUB(CURDATE(), INTERVAL 3 DAY), DATE_SUB(CURDATE(), INTERVAL 1 DAY))
mysql> create function PREV_BIZ_DATE ()
-> returns DATE
-> return (CURRENT_DATE() - interval if(DAYOFWEEK(CURRENT_DATE()) = 2, 3, 1) day);
mysql> SELECT * FROM daily_records WHERE date = PREV_BIZ_DATE();