[thelist] Displaying info according to date

John Corry john at neoncowboy.com
Thu Jun 26 03:00:57 CDT 2003


Oooh...Ooohh, I want a crack at this!!

You didn't mention what db you're using. I know how to do this in MySQL
and it's pretty simple:

The DATE_ADD and DATE_SUB functions work well for this purpose, but you
have to store your date in a date column type in the table (which is
probably the best format to store dates in anyway).

As an example, I have a function which returns all news articles that
are less than a month old, the relative part of the query is like this:
Note: CURRENT_DATE has been defined as the current date, duh.

...AND printdate >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH)...

<snip src="mysql manual 7.4.11">
DATE_ADD(date,INTERVAL expr type) 
DATE_SUB(date,INTERVAL expr type) 
ADDDATE(date,INTERVAL expr type) 
SUBDATE(date,INTERVAL expr type) 
These functions perform date arithmetic. They are new for MySQL Version
3.22. ADDDATE() and SUBDATE() are synonyms for DATE_ADD() and
DATE_SUB(). In MySQL Version 3.23, you can use + and - instead of
DATE_ADD() and DATE_SUB() if the expression on the right side is a date
or datetime column. (See example) date is a DATETIME or DATE value
specifying the starting date. expr is an expression specifying the
interval value to be added or substracted from the starting date. expr
is a string; it may start with a `-' for negative intervals. type is a
keyword indicating how the expression should be interpreted. The
EXTRACT(type FROM date) function returns the 'type' interval from the
date.
</snip>

Hth,
John

> What I'm trying to do, is add information to a table and then 
> retrieve everything up to a date point, I've tried a few 
> things like using $now = $date and using a formatted date in 
> the sql query, but no luck, I'm storing dates like this $date 
> = date("M jS Y"); you probably get the idea.



More information about the thelist mailing list