3

Right now I have a MySQL table with one column called "date_time" that simply lists different DATETIME values (e.g. "2010-11-30 12:55:00"). Is it possible for me to construct a query that directly:

  • Selects DATETIME values where the month is the current month?
  • Selects DATETIME values where the days aren't passed days (e.g. tomorrow and until the end of the month?

An abstract query in my mind would look like:

$query = "SELECT * FROM meetings WHERE uid = " . $_SESSION['uid'] . "AND the DATETIME month is active month AND the DATETIME day is bigger than the active day"

Is this functionality possible in MySQL (did some searching but didn't find anything helpful), or will I have to use PHP functions later to filter this stuff out? Many thanks in advance.

1
  • what's wrong with using WHERE date_time<NOW()? Commented Nov 17, 2010 at 10:33

2 Answers 2

5

MySQL has a ton of date formatting functions that would solve this for you:

Select by specific month...

SELECT ... WHERE MONTH(date_time) = MONTH(now());

Select by non-passed days:

SELECT ... WHERE DAY(date_time) > DAY(now());

Of course, these sorts of queries will pick out the same month/days in ANY records from ANY years. If you want to restrict to particular chunks of time, then you'll have to add more clauses.

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

1 Comment

I really need to improve my search skills, since I did not find that page. Many thanks for bringing this to my attention!
1

Something like:

$query = "SELECT * FROM meetings WHERE uid = " . $_SESSION['uid'] . "WHERE EXTRACT(month from now()) = extract(month from date_time) AND EXTRACT(day from now()) < extract(day from date_time)

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.