我有 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
感谢您的帮助。
最佳答案
按照@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
关于php - 比较 MySQL 数据库中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28079541/