Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nickdavies07's avatar

Sorting by human readable date when using DataTables

Not entirely a Laravel question but have asked this question on DT forum as well. Just thought someone may know!

Is there a way to sort a column by a human readable date?

I use the Carbon date package with the diffForHumans() method to convert my dates to human readable format i.e. 3 days ago, 5 minutes ago etc.

Datatables seems to sort numerically and alphabetically.

Example: https://screenshots.firefox.com/IYm9uC74bz0HaKq8/asset-manager.ngrok.io

Using Laravel's sortBy() and SortByDesc() doesn't seem to affect the column.

0 likes
4 replies
Tomi's avatar

If you are using Server-Side data source: https://datatables.net/manual/server-side

you can just send the column to the server for that is should be sorted.

If Your column is in the table i cant imagine that the sortBy() is not working.

If you are paginating the data you have tosortBy()->paginate().

nickdavies07's avatar

I've actually got the data in the view being pulled from the controller, not yet looked at using the server side data source but may be something I do at some point.

Figured out a solution anyway - disabled DataTables sorting using bSort: false so now using sortByDesc('created_at') sorts by the most recent record.

Finally!

1 like
Tomi's avatar

Also you could just select the data in diffForHumans() and normal format, and tell datatables that when you click the sort field it should just sort by a different column.

Im happy you figured it out tho. :)

1 like
BigDataMac's avatar

It's an old topic but I think the question is still valid and I've just spent some time on figuring this out. Assuming that lastChangeTime() returns diffForHumans() the easiest way to make things work is just to put two dates in a column and hide the first one, like that:

<td>
<span style="display:none">{{ $documentation->updated_at }}</span>
<span>{{ $documentation->lastChangeTime() }}</span>
</td>

This way DT will sort by first (hidden span) and users will see the second.

Please or to participate in this conversation.