Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Month and year datepicker problem in codeigniter

I have a problem in using a datepicker for my data. I need to input only the month and year for my database. This is my type for the data

enter image description here

So far i have this view:

<div class="form-group">
    <label for="Periode" class="col-md-2">
        Periode <text style="color:red"><b>*</b></text>
    </label>
<div class="col-md-4">
<script type="text/javascript">
    $(function() {   
        $("#periode").datepicker({
            dateFormat: 'YYYY-MM',
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,

            onClose: function(date, inst) {
                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                $(this).val($.datepicker.formatDate('MM yy', new Date(year, month, 1)));
            }
        });

        $("#periode").focus(function () {
            $(".ui-datepicker-calendar").hide();
            $("#ui-datepicker-div").position({
                my: "center top",
                at: "center bottom",
                of: $(this)
            });
        });
    });
</script>
<input class="form-control" name="periode" id="periode" data-format="YYYY-MM" class="Periode" required />

The datepicker is working (it shows only month and year in the display) enter image description here

But Once i clicked the submit button, the data is not successfully inputed into the database, i checked the data from xampp and it clearly shows 0000-00-00 in the table

Is there any better solution for the problem? already tried to look on another questions but i can't find any clear solution :/

EDIT: in case if you are wondering, this is the controller

        public function submit(){
    $data = array(
        'kode_karyawan'=>$this->input->post('kode_karyawan'),
        'nama_karyawan'=>$this->input->post('nama_karyawan'),
        'periode'=>$this->input->post('periode')
    );
    $this->absengaji_m->insert($data);
    redirect('absengaji');
   }
}

and this is the model:

   public function insert($data){
        print_r($data);
        $this->db->insert('uangmakan',$data);
    }
like image 241
Team Avatar asked Jan 30 '26 15:01

Team


1 Answers

This is a function to convert date from YYYY-MM to YYYY-MM-DD and then insert to database

function con2mysql($date) {
  $date = explode("-",$date);
  if ($date[2]<=9) { $date[2]="0".$date[2]; }
  $date = array($date[0], $date[1], $date[2]);

 return $n_date=implode("-", $date);
 }

If you are using PHP 5 >= 5.1.0 there is native funciton

$dateTime = new DateTime($yourDate);
$formatted_date = date_format($dateTime, 'Y-m-d' );

you need to change

public function submit(){
        $data = array(
            'kode_karyawan'=>$this->input->post('kode_karyawan'),
            'nama_karyawan'=>$this->input->post('nama_karyawan'),
            'periode'=>$this->input->post('periode')
        );
        $this->absengaji_m->insert($data);
        redirect('absengaji');
       }
    }

To

public function submit(){
     $dateTime = new DateTime($this->input->post('periode'));
     $formatted_date = date_format($dateTime, 'Y-m-d' );

        $data = array(
            'kode_karyawan'=>$this->input->post('kode_karyawan'),
            'nama_karyawan'=>$this->input->post('nama_karyawan'),
            'periode'=> $formatted_date
        );
        $this->absengaji_m->insert($data);
        redirect('absengaji');
       }
    }
like image 127
Sunny Kalariya Avatar answered Feb 01 '26 04:02

Sunny Kalariya