与 MySQL DATETIME 时间戳进行时间比较检查

发布于 2024-10-10 08:30:06 字数 78 浏览 0 评论 0原文

我想检查距离 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一花一树开 2024-10-17 08:30:06

你可以而且可能应该在 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.

掩饰不了的爱 2024-10-17 08:30:06

最简洁的方法是在 MySQL 中使用 DATE_SUB(或 DATE_ADD,具体取决于您的目标)。例如:

SELECT DATE_SUB(NOW(), INTERVAL 1 HOUR)

同样,您可以将“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:

SELECT DATE_SUB(NOW(), INTERVAL 1 HOUR)

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.

轮廓§ 2024-10-17 08:30:06

这个链接解释了 PHP >= 5.3 的 date_diff 和一些解决方法旧版本的解决方案。

This link explains about date_diff for PHP >= 5.3 and some work around solutions for older version.

残月升风 2024-10-17 08:30:06
$db_time = strtotime($row->timestamp_col);
$age = 60 * 60;
if ($db_time < (time() - $age)) {
    doSomething();
}

这是不必要的分解,因此您可以看到它是如何工作的。

$db_time = strtotime($row->timestamp_col);
$age = 60 * 60;
if ($db_time < (time() - $age)) {
    doSomething();
}

This is unnecessarily broken down so you can see how it works.

我们的影子 2024-10-17 08:30:06
$now = time();

$dbDate = strtotime($row['datetime']);

if ($now > $dbDate + 3600) {
   // yes
}
$now = time();

$dbDate = strtotime($row['datetime']);

if ($now > $dbDate + 3600) {
   // yes
}
久随 2024-10-17 08:30:06

您将支票放入查询中。然后您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文