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?

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.

Member Avatar for diafol

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)");

Yay Thanks for the help! ^_^

Member Avatar for diafol

We solved?

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 ;)

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.