PHP
Codeigniter

How To Get Current Month In Codeigniter?

We will learn how to get current month data in codeigniter. we can get current month data using codeigniter query builder. we will use MONTH() and YEAR() mysql function for getting current month data. I will share with you simple example example of getting current month data in codeigniter 3 application. sometime it might be require to get and display only current month data in your php codeigniter 3 application.

  • 4.5/5.0
  • Last updated 05 September, 2022
  • By Admin

You can see bellow example database query to getting current month data from database items table.

Example:
  1. <span class="pun">&lt;?</span><span class="pln">php </span>
  2. <span class="pln">$data</span><span class="pun">[</span><span class="str">'data'</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">db</span><span class="pun">-&gt;</span><span class="kwd">select</span><span class="pun">(</span><span class="str">'*'</span><span class="pun">)</span>
  3. <span class="pln"> </span><span class="pun">-&gt;</span><span class="kwd">where</span><span class="pun">(</span><span class="str">'MONTH(created_at)'</span><span class="pun">,</span><span class="pln"> date</span><span class="pun">(</span><span class="str">'m'</span><span class="pun">))</span>
  4. <span class="pln"> </span><span class="pun">-&gt;</span><span class="kwd">where</span><span class="pun">(</span><span class="str">'YEAR(created_at)'</span><span class="pun">,</span><span class="pln"> date</span><span class="pun">(</span><span class="str">'Y'</span><span class="pun">))</span>
  5. <span class="pln"> </span><span class="pun">-&gt;</span><span class="kwd">get</span><span class="pun">(</span><span class="str">"items"</span><span class="pun">)</span>
  6. <span class="pln"> </span><span class="pun">-&gt;</span><span class="pln">result</span><span class="pun">();</span>
  7. <span class="pln">print_r</span><span class="pun">(</span><span class="pln">$data</span><span class="pun">[</span><span class="str">'data'</span><span class="pun">]);</span>
  8. <span class="kwd">exit</span><span class="pun">;</span>
  9. <span class="pun">?&gt;</span>
Output:
  1. <span class="typ">Array</span>
  2. <span class="pun">(</span>
  3. <span class="pln"> </span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> stdClass </span><span class="typ">Object</span>
  4. <span class="pln"> </span><span class="pun">(</span>
  5. <span class="pln"> </span><span class="pun">[</span><span class="pln">id</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="lit">2</span>
  6. <span class="pln"> </span><span class="pun">[</span><span class="pln">title</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="typ">Codeigniter</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> CRUD </span><span class="typ">Example</span><span class="pln"> </span>
  7. <span class="pln"> </span><span class="pun">[</span><span class="pln">description</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="typ">Codeigniter</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> CRUD </span><span class="typ">Example</span><span class="pln"> </span><span class="typ">From</span><span class="pln"> </span><span class="typ">Scratch</span><span class="pln"> </span><span class="typ">To</span><span class="pln"> follow </span><span class="pun">:</span><span class="pln"> codewale</span><span class="pun">.</span><span class="pln">com</span>
  8. <span class="pln"> </span><span class="pun">[</span><span class="pln">created_at</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="lit">2019</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="lit">11</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span>
  9. <span class="pln"> </span><span class="pun">[</span><span class="pln">updated_at</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="lit">0000</span><span class="pun">-</span><span class="lit">00</span><span class="pun">-</span><span class="lit">00</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span>
  10. <span class="pln"> </span><span class="pun">)</span>
  11. <span class="pun">)</span>

I hope it can help you...