its comparing digits up to the first non-number
- 3 days ago
- 5 days ago
- 8 days ago
but will probably fail on '10 days ago' and '1 month ago'
Why are you doing this - it seems a strange way of sorting...
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
As far as the php docs are concerned in the method The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. But here is how I am using it with my posts
foreach ($actualPosts as $post) {
$post->sorter = [
'sorter_date_time' => $post->created_at->diffForHumans(),
'is_shared' => 'false',
'post_sharer' => null
];
}
In the above code I set the sorter_date_time to be string And then I use it inside usort
usort($actualposts, function ($post1, $post2) {
return $post2->sorter['sorter_date_time'] > $post1->sorter['sorter_date_time'];
});
AND IT GIVES THE DESIRED RESULT!!.It gives the most recently created posts . How is this possible if I am using a string for comparison inside usort and not an integer?
Please or to participate in this conversation.