title: Codeigniter & HMVC Calendar Not Inserting Correct Date?
body: <p>I am using the CodeIgniter Calendar <a href="http://www.codeigniter.com/user_guide/libraries/calendar.html" rel="nofollow noreferrer">class</a> and I can click on any day and enter text, <strong>but when I click on date in January 2016</strong> and the insert data it does not enter the correct date.</p>
  
  <p>It inserts 2015-12-01 and should insert date 2016-1-1 Just can not insert date correct if in next year.</p>
  
  <blockquote>
    <p><strong>Question</strong> It is not inserting the correct date to database if insert data is in a new year let say 2016, will only insert year date 2015 How could I solve this issue?</p>
  </blockquote>
  
  <p>Note: When I view month of Jan 2016 url says </p>
  
  <pre><code>http://localhost/project/admin/common/dashboard/2016/01
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <p>
  
  <pre><code>class Calendar extends MX_Controller {
  
      public function index() {
  
          $year = ($this-&gt;uri-&gt;segment(4)) ? $this-&gt;uri-&gt;segment(4) : 0;
  
          if (!$year) {
              $year = date('Y');
          }
  
          $month = ($this-&gt;uri-&gt;segment(5)) ? $this-&gt;uri-&gt;segment(5) : 0;
  
          if (!$month) {
              $month = date('m');
          }
  
          $this-&gt;load-&gt;model('admin/dashboard/model_calendar');
  
          $this-&gt;model_calendar-&gt;delete();
  
          if ($day = $this-&gt;input-&gt;post('day')) {
              $this-&gt;model_calendar-&gt;add_calendar_data("$year-$month-$day", $this-&gt;input-&gt;post('data'));
          }
  
          $data['calendar'] = $this-&gt;model_calendar-&gt;generate($year, $month);
  
          return $this-&gt;load-&gt;view('dashboard/calender_view', $data);
      }
  
  }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>&lt;?php
  
  class Model_calendar extends CI_Model {
  
      var $prefs;
  
      public function __construct() {
          parent::__construct();
  
          $this-&gt;prefs = array(
              'start_day' =&gt; 'monday',
              'show_next_prev' =&gt; true,
              'next_prev_url' =&gt; base_url('admin/common/dashboard')
          );
  
          $this-&gt;prefs['template'] = '
              {table_open}&lt;table border="0" cellpadding="0" cellspacing="0" class="table table-striped table-bordered calendar"&gt;{/table_open}
  
              {heading_row_start}&lt;tr&gt;{/heading_row_start}
  
              {heading_previous_cell}&lt;th&gt;&lt;a href="{previous_url}"&gt;&lt;i class="fa fa-chevron-left fa-2x "&gt;&lt;/i&gt;&lt;/a&gt;&lt;/th&gt;{/heading_previous_cell}
              {heading_title_cell}&lt;th class="text-center" colspan="{colspan}"&gt;{heading}&lt;/th&gt;{/heading_title_cell}
              {heading_next_cell}&lt;th class="text-right"&gt;&lt;a href="{next_url}"&gt;&lt;i class="fa fa-chevron-right fa-2x"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/th&gt;{/heading_next_cell}
  
              {heading_row_end}&lt;/tr&gt;{/heading_row_end}
  
              {week_row_start}&lt;tr&gt;{/week_row_start}
              {week_day_cell}&lt;td&gt;{week_day}&lt;/td&gt;{/week_day_cell}
              {week_row_end}&lt;/tr&gt;{/week_row_end}
  
              {cal_row_start}&lt;tr class="days"&gt;{/cal_row_start}
              {cal_cell_start}&lt;td class="day"&gt;{/cal_cell_start}
  
              {cal_cell_content}
                  &lt;div class="day_num"&gt;{day}&lt;span class="label label-danger" style="margin-left: 10px;"&gt;Notice&lt;/span&gt;&lt;/div&gt;
                  &lt;div class="content"&gt;{content}&lt;/div&gt;
              {/cal_cell_content}
              {cal_cell_content_today}
                  &lt;div class="day_num highlight"&gt;{day}&lt;/div&gt;
                  &lt;div class="content"&gt;{content}&lt;/div&gt;
              {/cal_cell_content_today}
  
              {cal_cell_no_content}&lt;div class="day_num"&gt;{day}&lt;/div&gt;{/cal_cell_no_content}
              {cal_cell_no_content_today}&lt;div class="day_num highlight"&gt;{day} &lt;span class="label label-success"&gt;Current Day&lt;/span&gt;&lt;/div&gt;{/cal_cell_no_content_today}
  
              {cal_cell_blank}&amp;nbsp;{/cal_cell_blank}
  
              {cal_cell_end}&lt;/td&gt;{/cal_cell_end}
              {cal_row_end}&lt;/tr&gt;{/cal_row_end}
  
              {table_close}&lt;/table&gt;{/table_close}
          ';
      }
  
      public function add_calendar_data($date, $data) {
          $this-&gt;db-&gt;select('date');
          $this-&gt;db-&gt;from($this-&gt;db-&gt;dbprefix . 'calendar');
          $this-&gt;db-&gt;where('date', $date);
          $check = $this-&gt;db-&gt;count_all_results();
  
          if (empty($data)) {
              $this-&gt;db-&gt;where('data', $data);
              $this-&gt;db-&gt;delete($this-&gt;db-&gt;dbprefix . 'calendar');
          }
  
          if ($check) {
  
              $this-&gt;db-&gt;where('date', $date);
              $this-&gt;db-&gt;update($this-&gt;db-&gt;dbprefix . 'calendar', array('date' =&gt; $date, 'data' =&gt; $data));
  
          } else {
  
              $calendar_data = array(
                  'date' =&gt; $date,
                  'data' =&gt; $data
              );
  
              $this-&gt;db-&gt;insert($this-&gt;db-&gt;dbprefix . 'calendar', $calendar_data);
  
          }
  
  
      }
  
      public function delete() {
          $this-&gt;db-&gt;where('data', " ");
          $this-&gt;db-&gt;delete($this-&gt;db-&gt;dbprefix . 'calendar');
  
      }
  
      public function get_calendar_data($year, $month) {
          $this-&gt;db-&gt;select('date, data');
          $this-&gt;db-&gt;from($this-&gt;db-&gt;dbprefix . 'calendar');
          $this-&gt;db-&gt;like('date', "$year-$month", 'after');
          $query = $this-&gt;db-&gt;get();
  
          $cel_data = array();
  
          foreach ($query-&gt;result() as $row) {
              $cel_data[substr($row-&gt;date, 8,2)] = $row-&gt;data;
          }
  
          return $cel_data;
      }
  
      public function generate($year, $month) {
          $cel_data = $this-&gt;get_calendar_data($year, $month);
  
          $this-&gt;load-&gt;library('calendar', $this-&gt;prefs);
  
          return $this-&gt;calendar-&gt;generate($year, $month, $cel_data);
      }
  }
  </code></pre>
  
  <p>Image December</p>
  
  <p><a href="https://i.stack.imgur.com/C0SwE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C0SwE.png" alt="enter image description here"></a></p>