Monjurul Hasan
Monjurul Hasan

Reputation: 11

MySql delete operation comparing datetime with current date time

I am new in PHP and MySql. I want to delete rows from a table where order_before passed the current date and time. the order_before is date/time type.

Upvotes: 0

Views: 830

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173652

Just add the condition in the statement, should be easy enough:

$sql = sprintf("DELETE FROM mytable WHERE order_before > '%s'", date('Y-m-d H:i:s'));
$db->exec($sql);

Upvotes: 1

ooXei1sh
ooXei1sh

Reputation: 3559

<?php
$db = new PDO('mysql:host=localhost;dbname=db','root','root');

if(filter_has_var(INPUT_POST, 'submit')) {
  $now = date("Y-m-d H:i:s");
  $r = $db->query("SELECT * FROM db WHERE datetime < '".$now."'");
  $n = $r->rowCount();
  if ($n){
    while($o = $r->fetchObject()) {
      $db->query('DELETE FROM db WHERE id = '.$o->id);
    }
  }
}

Maybe something like the above? (untested, and off the top).

Upvotes: 2

Related Questions