Forum Moderators: coopster

Message Too Old, No Replies

PHP scripts to diplay items from XYZ date til today

         

Adam_C

10:32 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



I'm trying to work out the SQL command script to display all items from a fixed time period til the current date.

Something like...

$sql_query = "SELECT * FROM mytable WHERE dateAdded like 'todays date - 60 days'";

Excuse my simple questions... looks like I'm setting up camp in this forum whilst learning PHP/SQL etc.

coopster

12:04 am on Feb 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



dateAdded
would need to be a date or date-and-time [mysql.com] column type. The following statement will return any rows where the
dateAdded
column is 60 days or more. Note that it will also return any rows where the
dateAdded
column has a zero value, such as 0000-00-00 or 0000-00-00 00:00:00.

SELECT * FROM mytable WHERE DATE_SUB [mysql.com](NOW() [mysql.com], INTERVAL 60 DAY) > dateAdded;
# If you don't want the zero values, specify so:
SELECT *
FROM mytable
WHERE dateAdded > 0
AND DATE_SUB(NOW(), INTERVAL 60 DAY) > dateAdded
;
# or you could use the TO_DAYS [mysql.com] function,
# which is a bit more intuitive than DATE_SUB:
SELECT * FROM mytable WHERE TO_DAYS(NOW()) - TO_DAYS(dateAdded) > 60;

In MySQL >= 4.1.1 there is a new date function that is pretty slick. It works on date or date-and-time expressions. I think the statement is more intuitive in the new function. Note that this statement will NOT return any rows where the
dateAdded
column has a zero value, such as 0000-00-00 or 0000-00-00 00:00:00.:

SELECT * FROM mytable WHERE DATEDIFF [mysql.com](NOW(), dateAdded) > 60;
# If you want the zero values, specify so:
SELECT *
FROM mytable
WHERE dateAdded = 0
OR DATEDIFF(NOW(), dateAdded) > 60
;