DataTables: Ordering by Epoch Time

Viewed 1211

How can I sort the dates in my datatable, which is initialized from HTML data in the DOM? My dates are epoch timestamps which I use moment.js to display as desired (example: July 17, 2017).

I should note that using either the data-order or data-sort attr does not work (as described here: https://datatables.net/examples/advanced_init/html5-data-attributes.html)

Sample PHP: (this is part of a loop to collect all returned rows from a DB -- i then echo out the entirety of $member_return in the view)

$member_return .= '<tr><td>'.$user->email.'</td><td>'.$user->first_name.'</td><td>'.$label-stat.'</td><td>'.$label-sub.'</td><td>'.(int)$user->created_on.'</td><td>--</td></tr>';

Sample HTML:

<tr>
    <td>fake1@email.com</td>
    <td>John2</td>
    <td>Active</td>
    <td>Active</td>
    <td>1500317481</td>
    <td>--</td>
</tr>
<tr>
    <td>fake2@email.com</td>
    <td>John2</td>
    <td>Active</td>
    <td>Active</td>
    <td>1500317482</td>
    <td>--</td>
</tr>
<tr>
    <td>fake3@email.com</td>
    <td>John3</td>
    <td>Active</td>
    <td>Active</td>
    <td>1500317483</td>
    <td>--</td>
</tr>

jQuery:

oTable = $('#view_members').DataTable({
    "columns": [
        { data: "email" },
        { data: "name" },
        { data: "status" },
        { data: "subscription" },
        {
            data: "date",
            type: "date",
            render: function(data){
                return moment.unix(data).format("MMMM DD, YYYY")
            }
        },
        { "sortable": false }
    ]       
})

Using this method, the dates are being sorted based on their date, example:

July 17, 2017
July 17, 2017
July 16, 2017
July 15, 2017
June 14, 2017
etc

However, my issue is that its not sorting by the epoch date. In the above sample, while the first two records may have both been created on July 17, 2017, the second in that list should actually be the first based on the epoch time (again, this is a sample).

The question:

How can I display a date in my desired format (Month Day, Year), while sorting by the epoch timestamp?

1 Answers
Related