Fetch Record in Last Month / Current Month In Codeigniter
Many times I have faced the issues when fetching records from databases in Codeigniter. The issue is how to fetch data/records for last month or the current month in Codeigniter. But here we have a very simple PHP MySQL code so that you can easily solve it. I also did same way, it works fine for me. Just have a look below codes.
Here Tbl_name is the table where the paid_date is the column. Suppose we need to fetch data last month payment or Current Month paid is paid by customers.
This is the example for Last Month Data:
1 2 3 4 5 |
$first_date = date('Y-m-d', strtotime('first day of last month')); $last_date = date('Y-m-d', strtotime('last day of last month')); $this->db->select('Tbl_name'); $this->db->where('paid_date BETWEEN "'.$first_date.'" AND "'.$last_date.'" '); $this->db->get(); |
This is the example for Current Month Data:
1 2 3 4 |
$this->db->select('Tbl_name'); $this->db->where('MONTH(paid_date)', date('m')); $this->db->where('YEAR(paid_date)', date('Y')); $this->db->get(); |