Search code examples
phpjavascriptjquerysql-serverhandsontable

Date format handsontable issue


I have a handsontable table :

                $("#old_tab_handsontable").handsontable({
                    data: data,
                    startRows: 1,
                    colHeaders: ['ID', 'datePers', 'dateArchive', 'dateAncienne', 'dateDef', 'IS'],
                    columns: [
                        {data: 'ID'},
                        {data: 'datePers', type: 'date'},
                        {data: 'dateArchive', type: 'date'},
                        {data: 'dateHist.old', type: 'date'},
                        {data: 'dateDef', type: 'date'},
                        {data: 'IS'}
                    ],
                    colWidths: [100, 100, 100, 100, 100, 100]
                });
    
All the lanes are completed with my database. I have a button which saves the change on the lane :
        $("#save").click(function() {
            var handsontable = $("#old_tab_handsontable").data('handsontable');
            console.log(handsontable.getData());
            $.ajax({
                url: "/Reporting/saveperspective",
                data: {'data': handsontable.getData()},
                type: 'POST',
                success: function() {
                    generateJsMessage("Les perspectives ont bien été modifiées", 'success');
                },
                error: function() {
                    generateJsMessage("Une erreur s'est produite", 'error');
                }
            });
        });
    

My problem is that if I save one date as 2012-02-01, when I will reload the page, the considered cell will show 2012-01-02. When i console.log(handsontable.getData()); It's still 2012-02-01, when I var_dump($_POST['data']), it's still 2012-02-01... My database (which I am not allowed to touch/modify/...) is on SQL Server.

My update controller is :

        public function saveperspectiveAction() {
            $this->_helper->getHelper('layout')->disableLayout();
            $this->_helper->viewRenderer->setNoRender();
            $data = $_POST['data'];
            _dump($_POST['data']);
            foreach ($data as $one):
                $this->reporting->updateListPerspectives($one['ID'], $one['datePers'], $one['dateArchive'], $one['dateDef'], $one['IS']);
            endforeach;
            return true;
        }
    

and my getter controller is

     public function getperspectiveAction() {
            $resultSql = $this->reporting->getListPerspectives();
            $this->_helper->getHelper('layout')->disableLayout();
            $this->_helper->viewRenderer->setNoRender();
            $tab = array();
            foreach ($resultSql as $oneline) {
                if ($oneline['dateHist.old'] == NULL) {

array_push($tab, array('ID' => $oneline['theDate_id'], 'datePers' => $oneline['datePers']->format('Y-m-d'), 'dateArchive' => $oneline['dateArchive']->format('Y-m-d'), 'dateHist.old' => " ", 'dateDef' => $oneline['dateDef']->format('Y-m-d'), 'IS' => $oneline['isCriteria'])); } else { array_push($tab, array('ID' => $oneline['theDate_id'], 'datePers' => $oneline['datePers']->format('Y-m-d'), 'dateArchive' => $oneline['dateArchive']->format('Y-m-d'), 'dateHist.old' => $oneline['dateHist.old']->format('Y-m-d'), 'dateDef' => $oneline['dateDef']->format('Y-m-d'), 'IS' => $oneline['isCriteria'])); } } print json_encode($tab); } </pre>

Solution

  • Try setting the date format:

                $("#old_tab_handsontable").handsontable({
                    data: data,
                    startRows: 1,
                    colHeaders: ['ID', 'datePers', 'dateArchive', 'dateAncienne', 'dateDef', 'IS'],
                    columns: [
                        {data: 'ID'},
                        {data: 'datePers', type: 'date', dateFormat: 'mm/dd/yy'},
                        {data: 'dateArchive', type: 'date', dateFormat: 'mm/dd/yy'},
                        {data: 'dateHist.old', type: 'date', dateFormat: 'mm/dd/yy'},
                        {data: 'dateDef', type: 'date', dateFormat: 'mm/dd/yy'},
                        {data: 'IS'}
                    ],
                    colWidths: [100, 100, 100, 100, 100, 100]
                });