Code Lover
Code Lover

Reputation: 8348

PHP MYSQL compare time with created in database

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){}

Upvotes: 1

Views: 139

Answers (3)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

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

https://www.php.net/manual/en/datetime.diff.php

Here is the working code

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

Upvotes: 1

aksu
aksu

Reputation: 5235

Try this:

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

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

Upvotes: 3

i-CONICA
i-CONICA

Reputation: 2379

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
}

Upvotes: 0

Related Questions