Skip to content

Instantly share code, notes, and snippets.

@sohelrana820
Last active February 7, 2023 16:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sohelrana820/861c36a511282335ce85 to your computer and use it in GitHub Desktop.
Save sohelrana820/861c36a511282335ce85 to your computer and use it in GitHub Desktop.
How to sort PHP multidimensional array by timestamp
// Feeds array with title and time
$feeds = array(
array(
'title' => 'Some Feeds.',
'time' => '2015-03-31 13:06:00'
),
array(
'title' => 'And another.',
'time' => '2015-03-23 13:06:00'
),
array(
'title' => 'Another Feed.',
'time' => '2015-03-27 13:06:00'
),
array(
'title' => 'And one more feed.',
'time' => '2015-03-20 13:06:00'
),
array(
'title' => 'And one more feed.',
'time' => '2015-03-25 13:06:00'
)
);
// Sorting array by time (DESC ORDER)
usort($feeds, function($firstItem, $secondItem) {
$timeStamp1 = strtotime($firstItem['time']);
$timeStamp2 = strtotime($secondItem['time']);
return $timeStamp2 - $timeStamp1;
});
var_dump($feeds);
// Sorting array by time (ASC ORDER)
usort($feeds, function($firstItem, $secondItem) {
$timeStamp1 = strtotime($firstItem['time']);
$timeStamp2 = strtotime($secondItem['time']);
return $timeStamp1 - $timeStamp2;
});
var_dump($feeds);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment