user3485335
user3485335

Reputation: 45

Getting MySQL results based on date by current month in Codeigniter

I currently get the last 30 days results like this.

public function commission_month(){

$status = 'A';  

$this->db->select_sum('LenderCommission');
$this->db->where(['Status' => $status ]) ;
$this->db->where('CompletedDate >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) ');
$query = $this->db->get('toutcome'); 
$result = $query->result();

return $result[0]->LenderCommission;   


}

What i am really trying to do is get results for this current month. And then when its the next month , its should do the same.

Upvotes: 0

Views: 4144

Answers (1)

gabe3886
gabe3886

Reputation: 4265

You can use a "like" query to get the information you need, assuming the dates are stored in a manner you can easily apply a like to.

Basically, the query will be something along the lines of ... AND CompletedDate LIKE 'YYYY-MM-%'... where YYYY and MM are valid year and months. This can be done in CodeIgniter by using the $this->db->like() builder:

$this->db->like('CompletedDate', date('Y-m'));

Your code then being:

public function commission_month(){

    $status = 'A';  

    $this->db->select_sum('LenderCommission');
    $this->db->where(['Status' => $status ]) ;
    $this->db->like('CompletedDate', date('Y-m'));
    $query = $this->db->get('toutcome'); 
    $result = $query->result();

    return $result[0]->LenderCommission;   
}

Upvotes: 3

Related Questions