-5

I have a field in my table called date and it stores via the NOW() function the date the entry was added.

I need to query the database to find entries within the last 30 days only.

Is this possible in a single query?

Somk
  • 11,579
  • 30
  • 94
  • 139

1 Answers1

1

Sure. Create a time window and you can limit by that date

$time = time() - (86400 * 30); // 86400 seconds in one day
$sql = 'SELECT * FROM table WHERE datefield > "' . date('Y-m-d H:i:s', $time) . '"'; 

That should yield you records within the last 30 days

Machavity
  • 29,816
  • 26
  • 86
  • 96
  • why not use the built in mysql functions? –  Nov 03 '13 at 00:09
  • I've always shifted my processing to PHP because the database tends to be the choke point in processing. If this is just a one-off thing it probably doesn't matter. But feel free to post the MySQL answer – Machavity Nov 03 '13 at 00:24