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

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)

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);
}
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');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With