Jump to content

Pulling data from mysql database (this today/week/month)


ukscotth

Recommended Posts

Hi,

 

I have a table that contains posts and each post has a datetime field.

 

Im trying to work out how I can show entries from today, this week and this month so I can have a link that shows all posts from today or this month etc.

 

Any one know how I can do this ? My current piece of code that pulls the data from the db looks like this :

 

$texts= mysql_query("SELECT * FROM submittedtexts Order by id DESC LIMIT " . (($page - 1) * 6) . ", 6");

 

I want to add a bit that acts like : WHERE date = today

 

 

Hope that makes sense, Im a bit of a newbie :(

 

Thanks in advance,

 

Scott

Link to comment
Share on other sites

sure.

 

Basically it just pulls the data from the database and displays it :

 

$texts= mysql_query("SELECT * FROM submittedtexts Order by id DESC LIMIT " . (($page - 1) * 6) . ", 6");

while ($got_texts = mysql_fetch_array($texts)){the code that simply displays the data}

Link to comment
Share on other sites

You really don't want to use the PHP date function for this if you can avoid it. MySQL has time/date functions built-in HERE that are faster and more efficient. In what format are you storing the date? Is it in a DATE or DATETIME type data field? If it is, for today's date:

 

// if the field is DATETIME . . .
SELECT field1, field2 FROM table WHERE some_field = 'some_value' AND DATE(date_field) = CURDATE()

// if the field is DATE . . .
SELECT field1, field2 FROM table WHERE some_field = 'some_value' AND date_field = CURDATE()

 

Then you can add a BETWEEN clause in conjunction with MySQL's DATE_SUB() to get the last 7 days, or last month, etc.

 

SELECT field1, field2 FROM table WHERE some_field = 'some_value' AND date_field BETWEEN DATE_SUB( CURDATE(), INTERVAL 7 DAY ) AND CURDATE()

Link to comment
Share on other sites

  • 4 weeks later...

thanks very much for the replies and sorry for the slow reply.

 

That looks great but instead of grabbing the last 7 days I need it to grab data from this week so it would be all posts since Monday. If that makes sense.

 

Would that be easy to do ?

 

Thanks again.

 

Scott

Link to comment
Share on other sites

To grab the posts since Monday is tricky. Since Sunday is easier, as DAYOFWEEK() returns 1 for Sunday, 2 for Monday etc. So you'd need to use a conditional statement, I think.

 

Lemme test out some code.

Link to comment
Share on other sites

Seems to work great. Sorry for being stupid but will this return posts from today or posts from this week ?

 

$sql = 'SELECT * FROM `submittedtexts` WHERE approved = 1 AND `date` > DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) Order by id DESC LIMIT '. (($page - 1) * 6) .', 6';

Link to comment
Share on other sites

You probably want to use where `date` >= DATE_SUB(...

 

That's for this week.

 

For a day, use

 

SELECT `whatever` FROM `table` WHERE DAY(`date`) = 20 AND MONTH(`date`) = 6 AND YEAR(`date`) = 2011

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.