How to format all of my date fields?

How to format all of my date fields?

mmcnair80mmcnair80 Posts: 83Questions: 21Answers: 7

I have a bunch of dates that I would like to format. Currently, they are all DateTime I want them to be just the date in formatted as DDMMYYY.

How would I do this? I'm using Serverside processing and initializing my tables like this:

<script type="text/javascript" class="init">
    $(document).ready(function () {
    
        // Setup - add a text input to each footer cell
        $('#DataTable tfoot th').each(function () {
            var title = $(this).text();
            $(this).html('<input type="text" placeholder="Search ' + title + '" />');
        });

        var table = $('#DataTable').DataTable({
                "lengthMenu" : [[25, 50, 75, 100, 150], [25, 50, 75, 100, 150]],
                "dom" : '<"top"Biflp<"clear">>rt<"bottom"ip<"clear">>',
                "buttons" : [{
                        extend : 'collection',
                        text : 'Selection',
                        buttons : ['selectAll', 'selectNone']
                    }, {
                        extend : 'collection',
                        text : 'Export',
                        buttons : ['excel', 'csv', 'pdf']
                    }
                ],
                "fixedHeader" : {
                    header : true,
                    footer : true
                },
                "select" : true,
                "processing" : true,
                "serverSide" : true,
                "ajax" : {
                    "url" : "./ServerSide.php",
                    "type": "POST"
                },
        initComplete: function() {
          var api = this.api();

          // Apply the search
          api.columns().every(function() {
            var that = this;

            $('input', this.footer()).on('keyup change', function() {
              if (that.search() !== this.value) {
                that
                  .search(this.value)
                  .draw();
              }
            });
          });
        }
      });
    });
</script>

This question has an accepted answers - jump to answer

Answers

  • mmcnair80mmcnair80 Posts: 83Questions: 21Answers: 7
    Answer ✓

    Never mind, I figured it out. I added a couple lines to my FilterSort.class.php file (a modified version of the ssp.class.php). I updated the function data_output to be this:

    static function data_output($columns,$data)
    {
        $DateFields = include 'DateFields.php'; //This returns an array with all of the Date columns
        $out = array();
        for($i=0,$ien=count($data);$i<$ien;$i++)
        {
            $row = array();
            for($j=0,$jen=count($columns);$j<$jen;$j++)
            {
                $column = $columns[$j];
                if(isset($column['Formatter']))
                {
                    echo "Formatter<br>";
                    $row[$column['dt']] = $column['Formatter']($data[$i][$column['db']],$data[$i]);
                }
                else
                {                    
                    $row[$column['dt']] = $data[$i][$columns[$j]['db']];
                    if(in_array($column['db'],$DateFields,TRUE) && $row[$column['dt']] != '') //This IF checks if the current column is in the DateFields array and if there is a value for it, then updates the output to the correct format.
                    {
                        $row[$column['dt']] = substr($data[$i][$columns[$j]['db']],5,2) . "/" . substr($data[$i][$columns[$j]['db']],8,2) . "/" . substr($data[$i][$columns[$j]['db']],2,2);
                    }
                }
            }
            $out[] = $row;
        }
        return $out;
    }
    
This discussion has been closed.