Sorting Unusual Date Format?

Sorting Unusual Date Format?

etalietali Posts: 2Questions: 1Answers: 0

Hi all,

I'm new to Datatables - I've been using the Charisma framework and it comes with them, so I've been muddling along as I go trying to get used to the features.

I had my dates formatted like this:

2015-07-26

And sorting by date worked fine. A user asked for the date format to be changed to this:

Sun 26th July 2015

And sorting, naturally, no longer works as users would expect it to.

Is there an easy way to fix the sorting? I'm guessing the route would be to have a hidden column with the date in the standard format, and bind the sorting to that when the user clicks on the date column? I'm not sure where to start with that, though, or even if I'm just making things too complicated.

Thanks.

Answers

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67

    One way could be to use datatables html5-data-attributes , inspect the html code of that table and notice the data-order attribute

  • etalietali Posts: 2Questions: 1Answers: 0

    Thanks daniel_r!

    I've tried that, and it doesn't appear to be having any effect (The table is generated from a MySQL query if that makes any difference). I was running 1.0.8 and I've updated to 1.10, but no joy.

    This is how the table is generated (inside a loop):

    <td data-order="<?php echo date("ymd",strtotime($row_RecordsetSubMember['memberdue'])); ?>"> 
    <?php $input=$row_RecordsetSubMember['memberdue'];
     echo date("D jS F Y",strtotime($input)); ?> 
    </td>
    

    Thanks again.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I use hidden column for sorting in cases like this. I posted an example a while ago:

    https://datatables.net/forums/discussion/25782/how-to-sort-using-a-hidden-column-in-1-10-0

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67

    not a php user but make sure you place miliseconds of your date in data-order just like in the example

  • allanallan Posts: 63,772Questions: 1Answers: 10,511 Site admin

    Another option is to use the date / time sorting plug-in from this blog post. It will work with virtually any format.

    Allan

This discussion has been closed.