PHP SELECT to query date field plus 6 hours - php

I would like to query a date field to select all entries that are equal to or greater than current time + 6 hours. The commented first entry works, but it is the second entry that I am battling with. I know this syntax is nowhere near correct.
//$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND date_added >= CURRENT_DATE ORDER BY order_id DESC";
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND CURRENT_TIME >= date_added +6 hours";

$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND NOW()>= DATE_ADD(date_added,INTERVAL 6 HOUR)";
This query will get your date_added value and add 6 hours to it and after will compare it with the CURRENT_TIME. I would suggest to use NOW() instead of CURRENT_TIME in mysql.

You're probably looking for the DATE_ADD (documentation) function. In your case:
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND your_date_field >= DATE_ADD(CURRENT_TIME, INTERVAL 6 HOURS)";
would likely do what you need it to.

try this query
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND CURRENT_TIME >= DATE_ADD(date_added, INTERVAL 6 HOUR)";

Related

MYSQL inserting INTERVAL DAY number dynamically? [duplicate]

This question already has answers here:
Understanding PDO Prepared Statements and Binding Parameters
(1 answer)
How do I bind an INTERVAL param with PDO?
(1 answer)
Include constant in string without concatenating
(14 answers)
Insert a PHP constant into a string
(2 answers)
Closed 3 years ago.
I understand that you can use CASE to select the value of another column to set the interval :
SELECT CASE some_column
WHEN "SOME_VALUE" THEN date_add(date, INTERVAL value DAY)
WHEN "SOME_OTHER_VALUE" THEN date_add(date, INTERVAL value DAY)
END
AS newDate
FROM table
But how can I pass that number in from my script (and not a value in the database).
For example, I have a constant my PHP script which grabs it's value from an .env file :
define("DOWNLOAD_DAYS", getenv("DOWNLOAD_DAYS")); // say this is 3 for example
And my query currently looks like this :
$sql = 'SELECT * FROM orders WHERE user_id = :user_id
AND status = :status
AND date_added > DATE_SUB(NOW(), INTERVAL 7 DAY)
ORDER BY date_added DESC';
How could I use the DOWNLOAD_DAYS constant to replace the 7 in this query?
I've tried ...
AND date_added > DATE_SUB(NOW(), INTERVAL DOWNLOAD_DAYS DAY)
... Which obviously doesn't work as INTERVAL expects a keyword, not a string or a variable.
And also tried binding the entire command with PDO...
$range = 'DATE_SUB(NOW(), INTERVAL ' . DOWNLOAD_DAYS . ' DAY)';
...
AND date_added > :range
...
$stmt->bindParam(':range', $range, PDO::PARAM_STR);
.. But it's having none of that.
Any suggestions?
You can just build the entire query using the constant:
$sql = 'SELECT * FROM orders WHERE user_id = :user_id
AND status = :status
AND date_added > DATE_SUB(NOW(), INTERVAL ' . DOWNLOAD_DAYS . ' DAY)
ORDER BY date_added DESC';
or you can bind to just the interval value
$sql = 'SELECT * FROM orders WHERE user_id = :user_id
AND status = :status
AND date_added > DATE_SUB(NOW(), INTERVAL :interval DAY)
ORDER BY date_added DESC';
$stmt->bindValue(':interval', DOWNLOAD_DAYS, PDO::PARAM_INT);

How to check if user have X records from last 1 hour? [PHP/MySQL]

I want to check if the user has 5 records in my MySQL database in the last hour. That´s how I am doing it now:
$link = mysqli_query($link, "SELECT * FROM find_points WHERE timestamp > '.time()-3600.' AND user_id = '1' ORDER BY id DESC");
if(mysqli_num_rows($link) >= 5) {
echo 'more than 5 results';
}
It looks like it should work, but it doesn't work...
Please use below query
SELECT * FROM find_points WHERE TIMESTAMPDIFF( hour, timestamp , now() ) > 1 AND user_id = '1' ORDER BY id DESC
You can read in manual about TimestampDiff
It can be used to run difference between 2 dates in various formats.
Please check the Demo on SqlFiddle
It shows how TimestampDiff returns result and you can use the same in WHERE clause.
Update
Based on your comment, that the timestamp is stored as Unix Timestamp, you could use the following query:
SELECT * FROM find_points WHERE TIMESTAMPDIFF( hour, FROM_UNIXTIME(timestamp) , now() ) > 1 AND user_id = '1' ORDER BY id DESC
FROM_UNIXTIME will convert your UNIX Timestamp to DateTime Format. You can then pass this to TIMESTAMPDIFF which will calculate difference and return the number of hours.
Hope this helps.
"SELECT * FROM find_points WHERE timestamp > DATE_ADD(NOW(), INTERVAL 1 HOUR) AND user_id = '1' ORDER BY id DESC"

Select date for today

Table: id, confess, user_ip, time, url, loves, hate
Time is like 1413040760
$q = mysql_query("SELECT * FROM confessions where time >= unix_timestamp(curdate() + interval 1 day)") or die(mysql_error());
I need the best confess of day order by loves limit 1. This show my only blank, no results.
You're querying records that happened later than one day from now - i.e., in the future. Presumably, you don't have any records like that. You can change the + interval 1 day to - interval 1 day in order to get records that occurred up to 1 day ago.
$q = mysql_query("SELECT * FROM confessions where time >= unix_timestamp(curdate() - interval 1 day)") or die(mysql_error());
EDIT:
To answer the question in the comment, yes, it's possible to sort by loves - hates - just slap on an order by clause:
$q = mysql_query("SELECT * " .
"FROM confessions " .
"WHERE time >= unix_timestamp(curdate() - interval 1 day) " .
"ORDER BY (loves - hates) DESC" ) or die(mysql_error());

select from table where day difference is less than some number

I am trying to grab all SQL entries WHERE Day difference between current date and expiration date is < than 4 days
My first approach was following:
$sql_i_requested = "SELECT *, (To_days(date_return)-TO_DAYS(NOW())) as daydif FROM ".$tbl_name."
WHERE (status!='completed' AND status!='canceled')
AND owner_id=".$owner_id."
AND daydif < 4
ORDER BY date_created DESC";
My second aproach is (according to SQL DateDifference in a where clause):
$sql_i_requested = "SELECT * FROM ".$tbl_name."
WHERE (status!='completed' AND status!='canceled')
AND owner_id=".$owner_id."
AND date_return > DateAdd(day, -3, getdate())
ORDER BY date_created DESC";
Neither of them work, so how do I select FROM table WHERE day_difference between "date_return" and now() is less than 4 days?
EDIT:
changed
AND daydif < 4
to
AND (To_days(date_return)-TO_DAYS(NOW())) < 4
and now it's working. Anyway, maybe you guys could suggest other solutions.
Using the DATEDIFF
WHERE DATEDIFF(date_return, now()) < 4
try:
SELECT DATEDIFF(date_return, NOW()) AS dayDiff;
and
having dayDiff < 4
Try this:
select *
from table
where DATEDIFF(day, present, future) < 4

mysql , between and limit together

I want to get all the rows between a certain date interval , with the result set being at most 50 records. What can be the query ? I continuously getting errors on:
$sql = "SELECT DISTINCT userId FROM ".$TableName.
" where time BETWEEN (NOW() - INTERVAL "
.$USER_COUNT_DURATION.
" MINUTE AND NOW()) LIMIT 0, ".$limit." ;";
looks like paranthesis problem. "MINUTE AND NOW())" should be "MINUTE) AND NOW()"
$sql = "SELECT DISTINCT userId FROM ".$TableName." where time BETWEEN (NOW() - INTERVAL ".$USER_COUNT_DURATION." MINUTE) AND NOW() LIMIT 0, ".$limit." ;";

Categories