比较mysql where子句中的日期是否相同

时间:2016-09-02 09:10:00

标签: php mysql date where-clause

$stmt = $dbh->prepare("SELECT * FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.id = t2.id WHERE t1.username = :user AND date_format(t2.date, 'Y-m-d') == :time");
$stmt->bindValue(':time', date_format($time, 'Y-m-d'), PDO::PARAM_STR);
$stmt->bindValue(':user', $user, PDO::PARAM_STR);

我有这段代码来检查日期是否相等。通过相同的方式同一天,如January 1, 1990 == January 1, 1990

但我得到了date_format() expects parameter 1 to be DateTimeInterface,

如果在mysql where子句

中当天比较日期的合适方法是什么

1 个答案:

答案 0 :(得分:0)

尝试使用mysql str_to_date

$stmt = $dbh->prepare("SELECT * FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.id = t2.id WHERE t1.username = :user AND str_to_date(t2.date,'%M %d, %Y')  == :time");
$stmt->bindValue(':time', str_to_date($time,'%M %d, %Y'), PDO::PARAM_STR);
$stmt->bindValue(':user', $user, PDO::PARAM_STR);