Jump to content

Select older than 7 days


jamiet757

Recommended Posts

How can I select from a database an entry that is older than 7 days?

 

Right now, the column "data" is what I believe to be a unix timestamp created using this:

 

mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"))

 

I need to figure out how to phrase the MYSQL query to select something where data > what? if I want it to be greater than 7 days.

Link to comment
https://forums.phpfreaks.com/topic/190282-select-older-than-7-days/
Share on other sites

i suppose whoever wrote that line never saw the function time. anyhow, a UNIX timestamp is measured in seconds. in order to get the UNIX timestamp that was exactly 7 days ago, you simply need to subtract the number of seconds in 7 days:

 

echo 'timestamp now is: '.time().'<br />';
echo 'timestamp 7 days ago was: '.(time() - 7*24*60*60);
// 7 days * 24 hours * 60 minutes * 60 seconds

so, would this work:

 

$sql="select folder from photos where published=3 and data>".time() - 7*24*60*60;

 

I am not sure if I have my syntax right, but I think that will select folder from photos where published is 3 and data is (the current time) -7 days.

so, would this work:

 

$sql="select folder from photos where published=3 and data>".time() - 7*24*60*60;

 

I am not sure if I have my syntax right, but I think that will select folder from photos where published is 3 and data is (the current time) -7 days.

 

have you run this to see what you get?

No, it didn't work. Here is an example of what is in the "data" field for something that was added a few minutes ago:

 

1264801797

 

The date it gives is Jan 29 2010, 15:49:57

 

Which is correct, so I don't know why the script isn't finding it. I did test the script without the time and it works, so I know that it does what it is supposed to when I don't specify a time.

I realized I had my > facing the wrong direction, I want to select where data is smaller than 7 days ago, however it still doesn't work.

 

Do I have to add the time() function anywhere, or is that already built into php?

 

Do I have the sytax right, should it be

 

$sql="delete from photos where published=3 and data<".time() - 7*24*60*60;

 

or do I need to change where the quotes are?

you need to put parentheses around the subtraction expression. otherwise, php concatenates the current timestamp onto the string, and then attempts to subtract 7*24*60*60 from the string itself.

 

when in doubt, always echo the query to see if it contains what you think it should.

$days = time() -  60*60*24*7  // 7 DAYS

$sql="SELECT folder FROM photos WHERE published=3 AND timestamp > $days";

 

maybe this is better??

I wrote it on the fly and isn't tested but looks right to me (most things that fail do work in my head but i'm tryin :D)

 

the row's timestamp should still be LESS THAN the timestamp from 7 days ago in order to be older.

Archived

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

×
×
  • 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.