Jump to content

PHP/MySql Date


c_pattle

Recommended Posts

I want to have a feature on a website I'm building where the user can select rows from a MySQL table based on it's age.  For example they could select a piece of content between 30-60 days old.  I'm going to do this using the following sequel

 

"WHERE article_date BETWEEN 2010-12-01 AND 2010-11-01"

 

However I'm not sure how to make this work.  If the date is 2010-12-12 what is the easiest way to subtract 30 days from this date?  Thanks for any help. 

Link to comment
Share on other sites

Based on your format, I'm assuming the article_date is stored as a yyyy-mm-dd format, otherwise you would have to use the timestamps...

 

Check the query part of that link

http://www.roseindia.net/sql/sql-between-datetime.shtml

 

It states:

Select * From Stu_Table
Where Stu_Dob Between '1984-01-01 00:00:00' And '1986-1-1 00:00:00';

 

Never used the between operator in SQL myself though, more on it here:

http://www.w3schools.com/sql/sql_between.asp

 

EDIT: Didn't read your post well enough, grr...

 

$now = mktime();

$then = $now - (60*60*24*30);

That would give you the timestamp of 30 days ago, so....

 

$thirtydaysago = date("Y-m-d", $then);

 

So overall, here we go:

$now = mktime();
$then = $now - (60*60*24*30);

$thirtydaysago = date("Y-m-d", $then);
$current = date("Y-m-d", $now);

mysql_query("SELECT * FROM `table` WHERE `article_date` BETWEEN '$current' AND '$thirtydaysago'");

 

Seem like what you need?

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.