与 MySQL DATETIME 时间戳进行时间比较检查
我想检查距离 MySQL 中最后一个 datetime
戳记是否已经过了一个小时。我如何使用 PHP 来做到这一点?
I want to check if it has been an hour since the last datetime
stamp in MySQL. How can I do this using PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以而且可能应该在 mysql 中完成这一切
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
您可以使用查询找到日期时间字段和当前时间之间的差异,并且将结果作为一行返回。
you could and probably should do this entire thing in mysql instead
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
you can find the difference between a datetime field and the current time using your query, and return the result as a row.
最简洁的方法是在 MySQL 中使用 DATE_SUB(或 DATE_ADD,具体取决于您的目标)。例如:
同样,您可以将“1 HOUR”替换为“1 DAY”或“1 MINUTE”等其他内容。我总是喜欢在数据库级别进行日期比较,因为大概日期是以通用格式和 GMT 偏移量存储的。
The cleanest way to do this is in MySQL using DATE_SUB (or DATE_ADD depending on your goals). For example:
Likewise you can replace "1 HOUR" with other things like "1 DAY" or "1 MINUTE" and so on. I always prefer to do date comparisons at the database level since presumably the dates are being stored in a common format and GMT offset.
这个链接解释了 PHP >= 5.3 的 date_diff 和一些解决方法旧版本的解决方案。
This link explains about date_diff for PHP >= 5.3 and some work around solutions for older version.
这是不必要的分解,因此您可以看到它是如何工作的。
This is unnecessarily broken down so you can see how it works.
您将支票放入查询中。然后您可以使用 MySQL 日期比较,而 PHP 不必知道或关心。
You put the check into the query. Then you can use a MySQL date comparison and the PHP doesn't have to know or care.