PHP / Mysql Datetime Comparison?

I have one problem, I'm trying to do something like this:

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

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

But I completely lost. I do not know how to do the operation in PHP.

I ask if I can select this datetime from my database and check if this datetime matches between the last 15 minutes of my server, my DB is Mysql.

I hope you got what I meant! Thank!


Finally, I do this thanks to Sammich and others, here I leave a fragment:

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

    //15 minuts 15*60 = 900
     if ($diff <= 900) {
        $seconds =  $diff;
     } else {
$seconds =  $diff;    
 }
+4
source share
4 answers

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

  • MySQL DATE.
  • MySQL .
  • MySQL , PHP.
  • , .

​​ . date , .

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

: DATE_SUB()

PHP:

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

, :

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

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
}
+3

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
    }
}
+1

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

Source: https://habr.com/ru/post/1541654/


All Articles