9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP PDOでdate、datetime型を扱う。

Last updated at Posted at 2016-11-13

PDOでdate、datetime型を扱う

PHPでデータベース操作する時に使うPDOでdate型のパラメーターにbindValue()で入れれないという事象が発生して、微妙にハマったので、メモ代わりに書いておきます。

## コード例

$sql  = "SELECT ... "
$sql .= "WHERE DATE_FORMAT(start_at, '%Y-%m-%d') = STR_TO_DATE(:date ,'%Y-%m-%d')";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":date", date("Y-m-d", strtotime($date)), PDO::PARAM_STR);

だめだった例

$sql  = "SELECT ... "
$sql .= "WHERE DATE_FORMAT(start_at, '%Y-%m-%d') = :date";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":date", $date, PDO::PARAM_STR);
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?