日期时间比较 PHP/Mysql?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23813201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:58:52  来源:igfitidea点击:

Datetime comparison PHP/Mysql?

phpmysqldatedatetimedifference

提问by Ralsho

I'm trying to make something like this:

我正在尝试做这样的事情:

if (datetime - system date > 15 minutes) (false)

if (datetime - system date <= 15 minutes) (true)

But I'm totally lost. I don't know how to make this operation in PHP.

但我完全迷失了。我不知道如何在 PHP 中进行此操作。

I'd like to see how I can pick that DateTime from my database and check if it's between the last 15 minutes of my server's time.

我想看看如何从我的数据库中选择 DateTime 并检查它是否在我服务器时间的最后 15 分钟之间。

The type of database is MySQL.

数据库类型为 MySQL。



Finally, I managed to do it thanks to Sammitch and the other ones, here i leave the snippet:

最后,感谢 Sammitch 和其他人,我设法做到了,在这里我留下了片段:

$now    = time();
$target = strtotime($row[1]);
$diff   = $now - $target;

// 15 minutes = 15*60 seconds = 900
if ($diff <= 900) {
    $seconds = $diff;
} else {
    $seconds = $diff;
}

回答by Sammitch

If your dates are already in MySQL you will want to do the comparison in the query because:

如果您的日期已经在 MySQL 中,您将需要在查询中进行比较,因为:

  1. MySQL has proper DATE types.
  2. MySQL has indexes for comparison.
  3. MySQL performs comparisons muchfaster than PHP.
  4. If you filter your data in the query then less, or no time is spent transferring superfluous data back to the application.
  1. MySQL 有适当的 DATE 类型。
  2. MySQL 有用于比较的索引。
  3. MySQL 执行比较速度比 PHP 快得多。
  4. 如果您在查询中过滤数据,则将减少或不会花费时间将多余的数据传输回应用程序。

Below is the most efficient form. If there is an index on the datecolumn it will be used.

下面是最有效的形式。如果date列上有索引,则将使用该索引。

SELECT *
FROM table
WHERE date > DATE_SUB(NOW(), INTERVAL 15 MINUTE)

Docs: DATE_SUB()

文档: DATE_SUB()

If you need to do it in PHP:

如果您需要在 PHP 中执行此操作:

$now = time();
$target = strtotime($date_from_db);
$diff = $now - $target;
if ( $diff > 900 ) {
  // something
}

or, more succinctly:

或者,更简洁地说:

if( time() - strtotime($date_from_db) > 900 ) {
  // something
}

回答by vascowhite

You have a solution for MYSQL in other answers, a good solution for PHP is:-

您在其他答案中有针对 MYSQL 的解决方案,针对 PHP 的一个很好的解决方案是:-

$now = new \DateTime();
$target = new \DateTime(getTimeStringFromDB());
$minutes = ($target->getTimestamp() - $now->getTimestamp())/60;
if($minutes < 15){
    // Do some stuff
} else {
    //Do some other stuff
}

回答by AamirR

the most efficient PHP/MySQL combined solution is:

最高效的 PHP/MySQL 组合解决方案是:

$date = date('Y-m-d H:i:s', strtotime('-15 minutes'));
$data = $pdo->query("SELECT date_field > '$date' as expired from table")->fetchAll(PDO::FETCH_COLUMN);
foreach($data as $expired) {
    if (!$expired) {
        // Still Valid
    }
}

回答by caiofior

Try this

尝试这个

$interval = date_create($date_from_db)->diff(new \DateTime());
if ($interval->format('%r%l')>15) {
    $result = false;
} else {
    $result = true;
}