Search code examples
phpmysqlsqldatecodeigniter

how to select complete month from a give date in php codeigniter


i have a codeigniter website where i am trying to display some results based on user id and date,

my database is like below:

enter image description here

i have a single date like this which will be selected by user: 2022-04-08

what i want is to get all the data from table of that complete month, if i get a single day date i want to fetch the complete month.

i did something like this:

  public function selectview($id,$date)
{
$this->db->select('*');
$this->db->from('delivered');
 $this->db->where("cid", $id);
$this->db->like('date', $date, 'after');
$query = $this->db->get();
$result = $query->result();
return $result;
}

however this doesnt give all the dates of that month, can anyone please tell me how to accomplish this, thanks in advance


Solution

  • You need to modify $date to just year and month. try something like this :

    public function selectview($id,$date)
    {
     //assuming your date format is YYYY-MM-DD
    $newDate = substr($date,0, strrpos($date, "-")); // 2022-04
    $this->db->select('*');
    $this->db->from('delivered');
    $this->db->where("cid", $id);
    $this->db->like('date', $newDate); //pass new date here
    $query = $this->db->get();
    $result = $query->result();
    return $result;
    }