php如何读取mysql最近1个小时的记录

WBOY
Release: 2016-06-06 20:51:23
Original
966 people have browsed it

时间字段的格式是这样的 2012-10-22 17:00:00

我想读取最近1个小时新增的记录,$time_limit = 1 * 60 * 60;

然后查询:

"SELECT * FROM Comments
WHERE TimeStamp > NOW() - $time_limit"
Copy after login
Copy after login

不过没有反应啊,问题出在哪了呢

回复内容:

时间字段的格式是这样的 2012-10-22 17:00:00

我想读取最近1个小时新增的记录,$time_limit = 1 * 60 * 60;

然后查询:

"SELECT * FROM Comments
WHERE TimeStamp > NOW() - $time_limit"
Copy after login
Copy after login

不过没有反应啊,问题出在哪了呢

在字段上用函数会到帐索引失效, 你可以这样做

换个思路解决

$timedate = date('Y-m-d H:i:s',time());
$hourtime = date('Y-m-d H:i:s',time()-3600);

SELECT * FROM Comments  WHERE TimeStamp  between $timedate  and $timedate;
Copy after login

TimeStamp为啥不建int,却用datetime类型的。其实楼上可以少一个条件,当前时间不需要做限制的。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!