我有一个DateTime,我在本月和上个月创建了两个变量,使用格式('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();
答案 0 :(得分:1)
哟可以使用:
$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();