PHP MYSQL与数据库中创建的时间进行比较

PHP MYSQL与数据库中创建的时间进行比较

问题描述:

I want to check if 30 min passed after created time in database. created is a time column having time stamp in this format 1374766406

I have tried to check with date('m-d-y H:i, $created) but than of course it is giving human readable output so don't know how to perform check if current time is not reached to 30min of created time.

Something like if(created > 30){}

我想检查数据库中 created code>时间后是否过了30分钟。 created code>是一个时间列,其格式为 1374766406 code> p>

我试图用 date('mdy)检查 H:i,$ created) code>但当然它提供了人类可读的输出,因此不知道如何检查当前时间是否达到 created code>时间的30分钟。

类似 if(created> 30){} code> p> div>

Try this:

$created = // get value of column by mysql and save it here.

if ($created >= strtotime("-30 minutes")) {
    // its over 30 minutes old
}

You need to use strtotime(); to convert the date in human form back to a timestamp, then you can compare.

EDIT: Maybe I misread.

So something like;

if(($epoch_from_db - time()) >= 1800){
    //do something
}

The better approach is to use DateTime for (PHP 5 >= 5.3.0)

$datenow = new DateTime();
$datenow->getTimestamp();

$datedb = new DateTime();
$datedb->setTimestamp(1374766406);

$interval = $datenow->diff($datedb);
$minutes = $interval->format('%i');

$minutes will give you the difference in minutes, check here for more

http://in3.php.net/manual/en/datetime.diff.php

Here is the working code

http://phpfiddle.org/main/code/jxv-eyg