date function myql query for expiry date option - php

I have a table named coupons. I want to display coupons which have expiry date coming soon. I am stuck with the query. Any suggestion please.
SELECT * FROM coupons WHERE c_date_expired BETWEEN
CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 5 DAY)
I want to return the last c_id i.e 3 because it is going to expire soon.

You forget to add Date in front of your Date_add:
SELECT * FROM coupons
WHERE c_date_expired >= CURDATE()
AND c_date_expired <= (CURDATE() + interval 5 day)

Related

How to display all records from today's UNIX_TIMESTAMP

I'm trying to display all attacks which have happened today. This is the current SQL I have in my php page. However it doesn't display how many have happened today.
$TodayAttacks = $odb->query("SELECT COUNT(id) FROM `logs` WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL '-1' DAY) AND UNIX_TIMESTAMP()")->fetchColumn(0);
If by "today" you mean "since midnight today", your query should look something like this:
SELECT COUNT(id) FROM logs
WHERE date >= CURDATE();
If there is a possibility of date values in the log greater than today's date (hey, weird stuff happens!) then you want this (I advise against using BETWEEN for date comparisons):
SELECT COUNT(id) FROM logs
WHERE date >= CURDATE()
AND date < DATE_ADD(CURDATE(), INTERVAL 1 DAY);
If you mean "within the last 24 hours", your query would look more like this:
SELECT COUNT(id) FROM logs
WHERE date >= DATE_SUB(NOW(), INTERVAL 1 DAY);
Hope this helps.
EDITED per comments below
If the date column is a Unix timestamp value (stored as INT(11)?) then the query should look something like the following:
SELECT COUNT(id) FROM logs
WHERE date >= UNIX_TIMESTAMP(CURDATE());
One could also do the following to get records from the last 24 hours:
SELECT COUNT(id) FROM logs
WHERE date >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY));
SQL Fiddle here.

How to make a date interval in a MySQL query?

I want to make a date interval in a MySQL query. The date is extracted dynamically from an HTML Form and I want to search if something is between 2 days after and after a fixed date. I've made an example but it's not working well.
SELECT * FROM `sessions` WHERE `start_date` = '2014-05-12'
or `start_date` between DATE_SUB(start_date, INTERVAL 1 DAY)
and DATE_ADD(start_date, INTERVAL 1 DAY)
Any other propositions ?
If I'm not wrong every time this query is resulting all rows from the sessions table. Let's take a look at this particular part of the query,
start_date between DATE_SUB(start_date, INTERVAL 1 DAY)
and DATE_ADD(start_date, INTERVAL 1 DAY),
So if the start_date value of a row is 22-07-2014 the query translates to,
start_date between 21-07-2014 and 23-07-2014)
So every row satisfies the condition as every date x is between x - 1th day and x + 1th day.
So if you want to execute the query to find a date between a certain interval, try
start_date between DATE_SUB('$start_date', INTERVAL 1 DAY)
and DATE_ADD('$start_date', INTERVAL 1 DAY)
instead. Note that '$start_date' is a php variable that is the date you are comparing the row with.

Fetch rows only from specific date in mySQL

I am trying to fetch rows only from specific date (like today, yesterday or 2 days ago) in mySQL. I have a column named "date" in my rows. (which includes dates like 1365053426).
$result=mysql_query("SELECT count(*) as total from track WHERE `date` >= CURRENT_DATE
AND `date` < CURRENT_DATE + INTERVAL 1 DAY");
I have tried this query, but it returns "0". What is the correct way to do that ?
how about using BETWEEN?
SELECT COUNT(*) as TotalCount
FROM Track
WHERE Date BETWEEN CURDATE() + INTERVAL -2 DAY AND CURDATE()
How about using datediff() function?
SELECT count(*) as total from track WHERE datediff(now(),date)=interval day
note: interval day could be declare from 0 -> up depends on what previous date you want to show

PHP MySQL - Select all where expiry date = todays date + 7 days

I am using PHPMyadmin and putting values in a database using PHP. I store the expiry date of products using a timestamp as follows,
FOR EXAMPLE:
2012-11-04
I want to select all where the expiry date is equal to todays date plus 8 days (such as the one above)
I also want to select all where expiry date is equal to todays date + 2 weeks in a seperate page if any one could help me out would be very grateful!
You can do that with a query like this:
SELECT * FROM table WHERE date = DATE_ADD(CURDATE(), INTERVAL 8 DAY)
You can use DATE_SUB for dates in the past.
Select all where the expiry date is equal to todays date plus 8 days
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 8 DAY)
Select all where the expiry date is equal to todays date plus 2 weeks
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 2 WEEK)
These docs will be helpful for you:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add

Filter results by timestamp for the past 24 hours

I am trying to filter the count of Rows found, per hour, over the past 24 hours.
I am using the following SQL query and 'landtime' is a DateTime field.
SELECT HOUR(landtime) as hour, COUNT(*) as total FROM ord_log WHERE landtime > DATE_SUB(NOW(), INTERVAL 24 HOUR) GROUP BY HOUR(landtime) ORDER BY HOUR(landtime)
Sample data set > http://pastebin.com/0rYBnePG
The results looked as expected, until I looked at the data it was using.
It was pulling dates from days/weeks before todays date when I only want a count for the past 24 hours.
Any help appreciated!
SELECT * FROM table WHERE day(data) = EXTRACT(day FROM (NOW() - INTERVAL 1 day))
No need to use a function, just use simple math:
SELECT HOUR(landtime) as hour, COUNT(*) as total FROM ord_log WHERE landtime > (CURRENT_TIMESTAMP - INTERVAL 1 DAY) GROUP BY HOUR(landtime) ORDER BY HOUR(landtime)

Categories