3

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);
    }
Team
  • 55
  • 9
  • hi, this is.. kind of hard (its date format after all).. just to be sure, when you insert your data into the [date field](https://dev.mysql.com/doc/refman/8.0/en/datetime.html) in the table the format is in `YYYY-MM-DD` right? did you do some conversion? mysql uses that `YYYY-MM-DD` format for insertion, so missing the day part may end up having the entire thing rejected or simply given default all zero value.. – Bagus Tesa Sep 11 '18 at 13:04
  • Change: new Date(year, month, 1) to: new Date(year, month) – Niall Lonergan Sep 11 '18 at 13:05
  • @BagusTesa Yes, the format is `YYYY-MM-DD` on my database. and yes, i actually have the exact same problem. can you help me? btw are you indonesian? hehe – Team Sep 11 '18 at 13:15
  • @NiallLonergan already tried it and its still not working :( – Team Sep 11 '18 at 13:16
  • Which datepicker are you using? Can you include it in your code or here? – Niall Lonergan Sep 11 '18 at 13:22
  • i used this datepicker: [link](https://stackoverflow.com/questions/2208480/jquery-ui-datepicker-to-show-month-year-only) check the second answer @NiallLonergan – Team Sep 11 '18 at 13:26
  • Does it work if in this line `$(this).val($.datepicker.formatDate('MM yy', new Date(year, month, 1)));` you change the date format to `yy-mm-dd`? – Salman A Sep 11 '18 at 13:28
  • Hang on, is the field in the database set with a default "0000-00-00) ? Because if so, it'll set the days to 00. – Niall Lonergan Sep 11 '18 at 13:29
  • @SalmanA its not working, they use that line for displaying the result in the form – Team Sep 11 '18 at 13:58
  • @NiallLonergan i didn't set any default – Team Sep 11 '18 at 13:59
  • I have updated my answer please check it – Sunny Kalariya Sep 11 '18 at 14:10

1 Answers1

2

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');
       }
    }
Sunny Kalariya
  • 336
  • 2
  • 9
  • interesting! But i am a real newbie in this kind of thing. So i was wondering, the function should be put in controller right? if the answer is yes, then how would i correlate the form and the function? sorry for asking too much questions tho – Team Sep 11 '18 at 13:23
  • At a time of inserting or updating date field in database that time to use this function – Sunny Kalariya Sep 11 '18 at 13:25
  • if my answer is help full to you then approve it – Sunny Kalariya Sep 11 '18 at 13:30
  • still confused by where should i put the function, can you tell me more about it? already update my question in case you want to know the model and controller – Team Sep 11 '18 at 14:03
  • Great... Also Thank you for answer approved – Sunny Kalariya Sep 11 '18 at 14:20