我正在尝试从日期在当前日期的两周内的数据库中提取行,我正在使用CodeIgniter,这是我的模型:
function GetToDoItems($userID)
{
$range = date('Y-m-d', strtotime("+2 weeks"));
$query = $this->db->query("SELECT * FROM FYP_Milestones
INNER JOIN FYP_Modules ON FYP_Milestones.ModuleID = FYP_Modules.ID
WHERE FYP_Milestones.MilestoneDue < $range
ORDER BY FYP_Milestones.MilestoneDue ASC
");
return $query->result();
}
它运行此查询:
SELECT * FROM FYP_Milestones INNER JOIN FYP_Modules ON FYP_Milestones.ModuleID = FYP_Modules.ID WHERE FYP_Milestones.MilestoneDue < 2016-04-14 ORDER BY FYP_Milestones.MilestoneDue ASC
考虑到2016-04-07比2016-04-14还要少7天,我期待单独行,但SQL返回空结果,为什么这是情况?
答案 0 :(得分:5)
你实际上不需要PHP的范围,使用SQL:
WHERE FYP_Milestones.MilestoneDue < (CURDATE() + INTERVAL 2 WEEK)
答案 1 :(得分:1)
在日期值中添加单引号,如:
$query = $this->db->query("SELECT * FROM FYP_Milestones
INNER JOIN FYP_Modules ON FYP_Milestones.ModuleID = FYP_Modules.ID
WHERE FYP_Milestones.MilestoneDue < '".$range."'
ORDER BY FYP_Milestones.MilestoneDue ASC
");