0

I have a table with these columns:

+------------------------------------------------------+
| ad_id | ad_title | active |   placed_on   | duration | 
+------------------------------------------------------+
|     1 | First    |      1 |  2010-19-10   |      365 |
+------------------------------------------------------+
|     2 | Second   |      1 |  2011-18-10   |      365 |
+------------------------------------------------------+

It's a table for listings that expire in 365 days from day it's placed_on and I wanted to create a PHP + SQL statement that would show listings that expire this month (e.g. first ad which was placed last year and expires today). How can I do math on those dates, like this 2010-19-10 + 365? and show the ones that expire this month. My brain froze after getting to:

mysql_query("SELECT `ad_id`, `ad_title`, `active` FROM `listings` WHERE `placed_on` = ``;

Is it even possible, if yes, please advise me. Thank you!

2 Answers 2

1

You can use adddate for your date arithmetic:

select ad_id, ad_title, active
from listings
where adddate(placed_on, concat(duration, ' days')) between '2011-10-01' and '2011-10-31'

Presumably you can supply the first and last days of the month from your PHP.

Sign up to request clarification or add additional context in comments.

Comments

0

Well if this is not your table and you can't make placed_on into something a little nicer to work with, you can generate your offset date on the front end and pass that to:

STRCMP()

http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#function_strcmp

WHERE STRCMP(placed_on,'2010-19-10') = 1 ORDER BY placed_on 

will give you everything newer than 2010-19-10.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.