比较MySQL数据库中的日期

时间:2015-01-22 00:42:32

标签: php mysql

在PHP方面经验丰富,我缺乏比较数据库中两个日期的方面。

我的数据库中存储了两个值。这只是unix时间戳。

价值1:post_time

价值2:expire_time

到期时间仅为发布时间后的7天。我只需要获得所有未过期的帖子"。

目前我一直在使用:if(($post['post_time']) >= time() - (60*60*24*7)),效果很好。但我更倾向于在查询中找到解决方案以获得结果。而不是拉出所有结果,然后在以后过滤它们。

这是我的问题:

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

就像@Dagon那样做:

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