简体   繁体   English

只需要从日期获取年份和月份

[英]Need to fetch only year and month from the date

Here my value inserted in to the table is like as follows, 在这里,我插入表中的值如下所示,

`2017-01-01`,
`2017-01-02`,
`2017-01-03`,
`2017-01-04`,
`2017-01-05`,
`2017-01-06`. 

i need to fetch only year and month from that, for that i used my code like this 我只需要从中获取年份和月份,因为我使用了这样的代码

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');
}

My $date value is 2017-01 and when i gave like this am getting error like this SELECT COUNT(*) AS numrows FROM tbl_attendance WHERE tbl_attendance . 我的$ date值是2017-01 ,当我给这样我得到的错误是这样SELECT COUNT(*) AS numRows行FROM tbl_attendance WHERE tbl_attendance . attendance_status = 1 AND tbl_attendance . Attenance_Status = 1 AND tbl_attendance . user_id = '1' AND 1970-01 = '2017-01' user_id = '1' AND 1'AND 1970-01 = '2017-01'

I believe you have error in line 我相信你有错误

$this->db->where(date('Y-m',strtotime($query)), $date);

checking value with value, 用价值检验价值,

For now replace your function with this, 现在,用此替换您的功能,

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');
}

Give it a try, it will work. 试试看,它将起作用。

First you need to parse the date into Ym format then you can use this column as condition 首先,您需要将日期解析为Ym格式,然后可以使用此列作为条件

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM