【发布时间】:2015-10-07 13:27:27
【问题描述】:
我有一个 DateTime,我创建了这个月和上个月的两个变量,使用 format('m') 来获取当月的数字。然后我将其放入 mysql 查询中,但在使用变量时查询不起作用,但如果我手动输入数字则起作用。我可以调用该变量,它会回显两个正确的日期,所以我有点不知道为什么它在 mysql 查询中不起作用。
有什么想法吗?
谢谢
$lastmonth = new DateTime();
$lastmonth->modify('-1 month');
$lastmonth = $lastmonth->format('m');
$thismonth = new DateTime();
$thismonth = $thismonth->format('m');
$thedate = "BETWEEN '2015-$lastmonth-16' AND '2015-$thismonth-15'";
$query = ("SELECT unid FROM responses WHERE (date $thedate)");
$stmt = $db->prepare( $query );
$stmt->execute();
我也试过了:
$lastmonth = new DateTime();
$lastmonth->modify('-1 month');
$lastmonth = $lastmonth->format('m');
$thismonth = new DateTime();
$thismonth = $thismonth->format('m');
$query = ("SELECT unid FROM responses WHERE (date BETWEEN '2015-$lastmonth-16' AND '2015-$thismonth-15')");
$stmt = $db->prepare( $query );
$stmt->execute();
【问题讨论】:
标签: php mysql variables datetime