Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sanjayacloud's avatar

Datatable date sort not work.

Hi I have use datatable in my project. I want to sort my date column. Its not work. But other column sort work fine. here's my code.

  $(document).ready(function () {
        $('#purchase-history').daterangepicker(null, function (start, end, label) {
            bringData( start.format('YYYY-MM-DD') , end.format('YYYY-MM-DD'));
        });
        bringData('{{date("Y/m/d" , strtotime("-1 months"))}}' , '{{date("Y/m/d")}}');
        function bringData(start, end) {
            $.ajax({
                url: base + '/admin/used-boxes-data',
                type: 'post',
                data: 'start='+start+'&end='+end,
                async: false,
                success: function (respond) {
                    if (respond.success) {
                        $('#content').html(respond.data);
                          $("#used").DataTable({
                                dom: "Blfrtip",
                                buttons: [
                                    {
                                        extend: "copy",
                                        className: "btn-sm"
                                    },
                                    {
                                        extend: "csv",
                                        className: "btn-sm"
                                    },
                                    {
                                        extend: "excel",
                                        className: "btn-sm"
                                    },
                                    {
                                        extend: "pdfHtml5",
                                        className: "btn-sm"
                                    },
                                    {
                                        extend: "print",
                                        className: "btn-sm"
                                    }
                                ],
                                responsive: true,
                              order: [[ 1, "desc" ]],
                              columnDefs : [{"targets":1, "type":"date-eu"}],
                            });

                    } else {

                    }
                },
                complete: function () {
                }
            });
        }
    });
0 likes
2 replies
bobbybouwmann's avatar

You're mixing php dates with javascript dates, that's not going to work. You need to pick one of them and keep using that. So either convert the php date to a javascript date and work from there or only use javascript dates.

For javascript dates moment js is the equal of carbon for php: https://momentjs.com/

sanjayacloud's avatar

I have used that. but still not work

here's the code.

  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
    <script src="https://cdn.datatables.net/plug-ins/1.10.12/sorting/datetime-moment.js"></script>
<script>
    $(document).ready(function () {
        $.fn.dataTable.moment( 'DD/MM/YYYY' );
        $('#purchase-history').daterangepicker(null, function (start, end, label) {
            bringData( start.format('YYYY-MM-DD') , end.format('YYYY-MM-DD'));
        });
        bringData('{{date("Y/m/d" , strtotime("-1 months"))}}' , '{{date("Y/m/d")}}');
        function bringData(start, end) {
            $.ajax({
                url: base + '/admin/used-boxes-data',
                type: 'post',
                data: 'start='+start+'&end='+end,
                async: false,
                success: function (respond) {
                    if (respond.success) {
                        $('#content').html(respond.data);
                          $("#used").DataTable({
                              ordering: true,
                              paging: true,
                              searching: true,
                                dom: "Blfrtip",
                                buttons: [
                                    {
                                        extend: "copy",
                                        className: "btn-sm"
                                    },
                                    {
                                        extend: "csv",
                                        className: "btn-sm"
                                    },
                                    {
                                        extend: "excel",
                                        className: "btn-sm"
                                    },
                                    {
                                        extend: "pdfHtml5",
                                        className: "btn-sm"
                                    },
                                    {
                                        extend: "print",
                                        className: "btn-sm"
                                    }
                                ],
                                responsive: true,
                                order: [[ 2, "desc" ]],
                            });

                    } else {

                    }
                },
                complete: function () {
                }
            });
        }
    });
</script>

Any idea?

Please or to participate in this conversation.