Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO::PARAM for dates?

Does some PDO::PARAM_??? exist which can be used for dates or timestamps?

Sample code:

$sql = "UPDATE my_table SET current_date = :date WHERE id = 43"; $statement = $pdo->prepare ($sql); $statement->bindValue (":date", strtotime (date ("Y-m-d H:i:s")), PDO::PARAM_STR); $statement->execute (); 
like image 349
vitto Avatar asked Mar 03 '10 20:03

vitto


2 Answers

When writing a date in an SQL query, you are writing it as a string; you have to do the same with prepared statements, and use PDO::PARAM_STR, like you did in the portion of code you proposed.

And for the "timestamp", if by "timestamp" you mean:

  • The MySQL timestamp data-type: it's the same, you'll pass it as a string
  • The PHP Unix timestamp, which is an integer: you'll pass it an int.
like image 190
Pascal MARTIN Avatar answered Sep 24 '22 19:09

Pascal MARTIN


Simply creating the date using php date function should fix this issue for you.

$handle->execute(array(":date"=>date("Y-m-d H:i:s", strtotime($date)), PDO::PARAM_STR)); 

Edit: Please note though, that strtotime (http://php.net/manual/en/function.strtotime.php) can't handle every kind of date formats.

like image 23
Rolty Avatar answered Sep 26 '22 19:09

Rolty