1

Being experienced in PHP, I lack in the area of comparing two dates in database.

I have two values stored in my database. Which are simply just unix time stamps.

Value 1: post_time

Value 2: expire_time

The expire time is just 7 days after the posted time. I simply need to get all posts that haven't "expired".

For the time being I've been using: if(($post['post_time']) >= time() - (60*60*24*7)), which works fine. But I'd much rather find a solution right in the query to get the results. Rather than pull all results and then filter them later.

Here is my query:

SELECT id, uid, title, description, 
       price, image, tags, post_type, 
       category, post_time, expire_time 
  FROM posts 
 ORDER BY RAND() LIMIT 8 

Your help is appreciated.

19
  • 1
    convert to mysql syntax: .. WHERE post_time >= UNIX_TIMESTAMP(NOW()) - (60*60*24*7) Commented Jan 22, 2015 at 0:44
  • Hmm let me try that @Dagon Commented Jan 22, 2015 at 0:46
  • 1
    @kingkero, it is, i just thought it was clearer Commented Jan 22, 2015 at 0:46
  • if leap seconds\hours\days\years matter to you, this is not the correct approach Commented Jan 22, 2015 at 0:48
  • 1
    the real date functions instead of basic arithmetic: dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html Commented Jan 22, 2015 at 0:53

1 Answer 1

2

Just do it the way @Dagon did it:

SELECT id, uid, title, description, 
       price, image, tags, post_type, 
       category, post_time, expire_time 
  FROM posts WHERE post_time >= UNIX_TIMESTAMP(NOW()) - (60*60*24*7) 
 ORDER BY RAND() LIMIT 8
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.