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

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

Related

date function myql query for expiry date option

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)

get sum of values from mysql table by month starting current month

Hi i currently have two tables, one has daily entries and other gets the total of those and stores monthly values.
I'm able to get totals of each month and year with below query, but what i'd like to do is only get the months where month is greater than and equal to current month. btw date column is in date format (yyyy-mm-dd)
$revbyMonth = $conn->prepare("SELECT EXTRACT(MONTH FROM date) as month, EXTRACT(YEAR FROM date) as year, SUM(rev) as total FROM {$tempName} GROUP BY year, month");
You want to add a where clause immediately after the from clause. I think this will work for you. If the date has no time component:
where date > date_sub(curdate(), interval day(curdate()) day)
The expression date_sub(curdate(), interval day(curdate()) day) gets the last day of the previous month. If the dates have a time component:
where date >= date_sub(curdate(), interval day(curdate()) - 1 day)
should work.
Note: this is better than other methods that process date using functions, such as:
where year(date) > year(curdate()) or
(year(date) = year(curdate()) and month(date) >= month(curdate()) )
The use of the functions prevents MySQL from using an index on the date column.

MySQL Query - Records between 15 days ago End_date to till End_date

I want to return records from database 15 days old from end _date to till end_date!
I am searching for the query for last 3 days!
However. I want your help to do a query. Its simple but I'm not sure how to do it.
I wrote query something like :
SELECT *
FROM bid_post
WHERE ending_date
BETWEEN DATE_SUB( DATE(`ending_date`) , INTERVAL 15
DAY )
AND ending_date >= CURDATE()
But it is not working !
The data column is a Varchar type. I am storing date as YYYY-mm-dd format
Does somebody now how can I accomplish this?
Thanks.
Please try with this query
SELECT *
FROM bid_post
WHERE ending_date between DATE_SUB( CURDATE() , INTERVAL 15 DAY )
AND CURDATE()
You should never store dates as varchar since these are not real dates and you need to store them using mysql native date data types.
Since the format is Y-m-d you may not need to do a conversion using str_to_date function and can use the query as
select * from bid_post
where
ending_date between date_sub(curdate(),interval 5 day) and curdate()
This will give you data from last 15 days till today.
Using conversion to real date you need str_to_date as
select * from bid_post
where
str_to_date(ending_date,'%Y-%m-%d') between date_sub(curdate(),interval 5 day) and curdate() ;
DEMO

Show dates between now and 4 weeks later

How to select dates from mysql table 'Courses' between now and 4 weeks later where Open='1' and sort by Date and Time?
SELECT * FROM Courses
WHERE Open='1' AND Date BETWEEN CURDATE() AND CURDATE() + INTERVAL 4 WEEK
ORDER BY Date, Time
Not tested

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

Categories