user9741470
user9741470

Reputation:

Check if date is expired SQLite

How I can fix this SQL query to check if a date is older than the actual and is expired and can be deleted?

I've tried with this code but it seems not work

<?php
$date = new DateTime();
      $d = $date->format('Y-m-d');
      $stmt = $this->db->prepare("DELETE FROM test_table WHERE _out = ? AND _out <= {$d}");
      $stmt->execute(array($d)); 
?>

I've also tried with this query but when the ajax call is made to the controller it seems not work

$stmt = $this->db->prepare("DELETE FROM reservations WHERE check_out <= {$d}");

Upvotes: 2

Views: 989

Answers (2)

user9741470
user9741470

Reputation:

After reading with attention the SQLite documentations I discovered that the best way to achieve my task is to use the date() function inside the query. I'm not too experienced with this database type. So here is how I fixed the issue

 <?php
   $stmt = $this->db->prepare('DELETE FROM test_table WHERE _out <= date()');
   $stmt->execute();
 ?>

Upvotes: 3

Danilo Souza Mor&#227;es
Danilo Souza Mor&#227;es

Reputation: 1593

<?php
$date = new DateTime();
$d = $date->format('Y-m-d');
$stmt = $db->prepare('DELETE FROM reservations WHERE check_out <= :out');
$stmt->bindValue(':out', 1, SQLITE3_TEXT);
$stmt->execute();
?>

Use ? for the replacement value and pass the valur in the execute method.

Upvotes: 0

Related Questions