0

Hi,

My codes at the moment are

mysql_query("SELECT * FROM table WHERE date = CURDATE()");

It outputs the correct stuff that i want to show that has been uploaded that day (newest) but I want it to show it for 3 days or maybe even a week...

Is there a way with php to make this happen?

4
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by Stefano Mtangoo
0

You could do something as simple as:

$date = date('Y-m-d', strtotime('3 days ago'));
$sql = "SELECT * FROM `table` WHERE `date` >= '{$date}'";

// OR

$date = date('Y-m-d', strtotime('7 days ago'));
$sql = "SELECT * FROM `table` WHERE `date` >= '{$date}'";

R.

0

You could even try this:

mysql_query("SELECT * FROM `table` WHERE `date` = DATE_ADD(CURDATE(), INTERVAL -3 DAY)");

You could make it like this too:

$no_days_ago = 4; //or take it from a search box

mysql_query("SELECT * FROM `table` WHERE `date` = DATE_ADD(CURDATE(), INTERVAL -{$no_days_ago} DAY)");

Edited by diafol: n/a

0

if you want three latest regardless of date you could go with some query like

$no_days_ago = 3; //or take it from a search box
mysql_query("SELECT * FROM `table` ORDER BY date_Column_Name LIMIT= {$no_days_ago}");

Not tested though ;)

0

if you want three latest regardless of date you could go with some query like

$no_days_ago = 3; //or take it from a search box
mysql_query("SELECT * FROM `table` ORDER BY date_Column_Name LIMIT= {$no_days_ago}");

Not tested though ;)

Order by sorts in ascending order by default, so your code would return the first three, not the last three.

0

Order by sorts in ascending order by default, so your code would return the first three, not the last three.

you are right ;)

mysql_query("SELECT * FROM `table` ORDER BY date_Column_Name DESC LIMIT= {$no_days_ago}");

also untested

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.