bart2puck
bart2puck

Reputation: 2522

DataTables sort by date function not working as desired

I have a table that has a column in unixtime. I have a seconds->hours function i run to convert 125193852379857 to 1 day, 59 hours, 9 minutes and 26 seconds.

The problem is the Datatable's sort function. if i have 3 values it would sort them as follows:

 1 day 2 hours 58 minutes and 26 seconds
 1 day 2 hours 59 minutes and 29 seconds
 1 day 2 hours 9 minutes and 42 seconds

I see why because it treats it as a "5" and not "58,59".

the conversion function:

function secondsToTime($seconds) {
 $dtF = new DateTime("@0");
 $dtT = new DateTime("@$seconds");
 return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
 }

the table:

<tr><th>Total Time</th><td>secondsToTime(1209571298712)</td></tr>
<tr><th>Total Time</th><td>secondsToTime(1598173598862)</td></tr>
<tr><th>Total Time</th><td>secondsToTime(5283749872348)</td></tr>

Is there a way around this?

Upvotes: 1

Views: 139

Answers (2)

Akshay Hegde
Akshay Hegde

Reputation: 16997

Make use of data-order or data-sort attributes

For example :

    <td data-order="1303686000">Mon 25th Apr 11</td>

Read more from here

Upvotes: 1

Natalia Chilikina
Natalia Chilikina

Reputation: 9

Good day!

Sort is working correctly, because by default it uses regular sort and sorts your values as string (see here). You can try to use sort with SORT_NATURAL or natsort

Upvotes: 0

Related Questions