此处插入表中的值如下所示,
`2017-01-01`,
`2017-01-02`,
`2017-01-03`,
`2017-01-04`,
`2017-01-05`,
`2017-01-06`.
我需要从中获取年份和月份, 因为我使用了像这样的代码
public function present_report_by_empid($user_id = null,$date = null)
{
$query='tbl_attendance.date_in';
$this->db->where('tbl_attendance.attendance_status', 1);
$this->db->where('tbl_attendance.user_id', $user_id);
$this->db->where(date('Y-m',strtotime($query)), $date);
return $this->db->count_all_results('tbl_attendance');
}
我的$ date值是2017-01
,当我这样给出时,我会收到这样的错误
SELECT COUNT(*) AS
{numRows行{1}} {tbl_attendance {1}} {tbl_attendance {1}} {attendance_status {1}} {tbl_attendance {1}} {USER_ID {1}} 1970至01年FROM
< / p>
答案 0 :(得分:3)
我相信你有错误
$this->db->where(date('Y-m',strtotime($query)), $date);
用值检查值,
现在用这个替换你的功能,
public function present_report_by_empid($user_id = null,$date = null)
{
$temp = explode("-",$date);
$query='tbl_attendance.date_in';
$this->db->where('tbl_attendance.attendance_status', 1);
$this->db->where('tbl_attendance.user_id', $user_id);
$this->db->where("YEAR(tbl_attendance.date_in)",$temp[0]);
$this->db->where("MONTH(tbl_attendance.date_in)",$temp[1]);
return $this->db->count_all_results('tbl_attendance');
}
尝试一下,它会起作用。
答案 1 :(得分:0)
首先,您需要将日期解析为 Y-m 格式,然后您可以将此列用作条件
public function present_report_by_empid($user_id = null,$date = null)
{
$query='tbl_attendance.date_in';
$this->db->select('tbl_attendance.*,DATE_FORMAT(tbl_attendance.date_in,"%Y-%m") as date_parse');
$this->db->where('tbl_attendance.attendance_status', 1);
$this->db->where('tbl_attendance.user_id', $user_id);
$this->db->where('date_parse',date('Y-m',strtotime($date)));
return $this->db->count_all_results('tbl_attendance');
}