简体   繁体   English

在MySQL数据库中比较日期

[英]Comparing dates in MySQL Database

Being experienced in PHP, I lack in the area of comparing two dates in database. 在PHP方面经验丰富,我缺乏比较数据库中两个日期的领域。

I have two values stored in my database. 我在数据库中存储了两个值。 Which are simply just unix time stamps. 这仅仅是Unix时间戳。

Value 1: post_time 值1: post_time

Value 2: expire_time 值2: expire_time

The expire time is just 7 days after the posted time. 过期时间仅是发布时间之后的7天。 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. 目前我一直在使用: if(($post['post_time']) >= time() - (60*60*24*7)) ,它可以正常工作。 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. 感谢您的帮助。

Just do it the way @Dagon did it: 就像@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM