Search code examples
codeignitermomentjsgroup-concat

Change group concat in mysql date to Indonesian date format using moments js


I'm going to convert date which came from database to locale date format (Indonesia) using moments. js in modal bootstrap by event click using codeigniter framework.

This is my schema database design enter image description here This is my view js by event click

 $('.pengajuan-cuti').on("click",function(event) {
    event.preventDefault();
    var id = $(this).data('id');
    $.ajax({
            url: base_url + "pengajuancuti/detail/" + id,
            type: "GET",
            dataType: "JSON",
            success: function(data) {
             //change single date to indonesia date 
            var cu_propose_date=data.tanggal_cuti;
            var cu_propose_date1 = new moment(cu_propose_date).format('DD-MM-YYYY');
            change date came from GROUP_CONCAT mysql
            var tanggal_cuti=data.tanggal_cuti;
            var tanggal_cuti1 = new moment(tanggal_cuti).format('DD-MM-YYYY');
            $('[name="tanggal_ambil_cuti"]').val(data.tanggal_cuti);
            $('[name="cu_propose_date"]').val(cu_propose_date1);
            $('#cu_status_approve').val(tanggal_cuti1).attr("selected", "selected");
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert('Error get data from ajax');
            }
        });

Then this is my controller

public function detail($id)
{
    $data = $this->Pengajuan_model->getemployeedetail($id);
    echo json_encode($data);
}

Then this is my model

function getemployeedetail($id)
{
    $query = $this->db->query("SELECT *, GROUP_CONCAT(cu_date_date SEPARATOR ',') as tanggal_cuti FROM cu_cuti a, cu_employee b, cu_date_cuti c where a.cu_id_cuti_employee=b.cu_id_employee 
    and a.cu_cuti_date_id=c.cu_date_cuti_id and a.cu_id_cuti='$id'");
    return $query->row();
}

This is my results date wrong pengajuan cuti, enter image description here

pengajuan cuti values are stored in database like this : 2016-02-14,2016-02-16,2016-02-17,2016-02-18

I know I'm wrong to using moment js but how can I make group concat works in single date as well?

This is my simple demo


Solution

  • after digging one hours and leave it one days, i found another ways to convet it all to indonesia date using convert mysql date, so i change the model bellow here

     function getemployeedetail($id)
        {
            $query = $this->db->query("SELECT *, GROUP_CONCAT( DATE_FORMAT( cu_date_date, '%d-%m-%Y' ) SEPARATOR ',') as tanggal_cuti FROM cu_cuti a, cu_employee b, cu_date_cuti c where a.cu_id_cuti_employee=b.cu_id_employee 
            and a.cu_cuti_date_id=c.cu_date_cuti_id and a.cu_id_cuti='$id'");
            return $query->row();
        }
    

    Hope this help poor programmers in the future !