Jump to content

Archived

This topic is now archived and is closed to further replies.

Leovenous

Query from the last 7 days

Recommended Posts

I would think this is both a common and easy question, but 2 hours of digging have not yielded any results yet, so here goes.

All I want, is a simple query to get data from a table that has been modified in the last 7 days.

I have a field called 'modified' that has a time stamp 0000-00-00 00-00-00. It seems to me I want a way to check the timestamps and grab which ones are less than 7 days old.

So the query might look something like:

[code]SELECT * FROM players WHERE modified  > (SUBDATE(CURDATE(), INTERVAL '7' DAY)[/code]

But obviously I'm bumping around in the dark.

Any help?

Share this post


Link to post
Share on other sites
one way

[code]<?php
$date_7 = date('Y-m-d', strtotime('-7 days'));

$sql = "SELECT .... WHERE modified >= '$date_7' ";
?>[/code]

Share this post


Link to post
Share on other sites
[code]SELECT * FROM players WHERE modified >= NOW() - INTERVAL 7 DAY[/code]

Share this post


Link to post
Share on other sites
Yup, sorry, my bad.

Share this post


Link to post
Share on other sites
Yep. That worked, and it makes sense. I wonder why I have not been able to find examples of queries even similar. (shrugs)

Thanks a bunch guys. Here's the final query:

[code]$query = "SELECT * FROM players WHERE Creator_ID LIKE '$id' AND modified >= CURDATE( ) - INTERVAL 7 DAY";[/code]

Share this post


Link to post
Share on other sites
Personally, I'd use NOW(), not CURDATE(), in case you ever change your modified field to be of type DATETIME, not DATE.  FYI, make sure to always use the field name as the lvalue, so that an index can be used, if available.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.