pesanan array php berdasarkan tanggal
usort($array, function($a, $b) {
return new DateTime($a['datetime']) <=> new DateTime($b['datetime']);
});
Wandering Wolverine
usort($array, function($a, $b) {
return new DateTime($a['datetime']) <=> new DateTime($b['datetime']);
});
<?php
// Declare multidimensional array
//initialization
$array = Array (
Array (
"test" => "TEST_1",
"datetime" => "2019-02-22 11:29:45",
),
Array (
"test" => "TEST_2",
"datetime" => "2019-02-13 11:29:45",
),
Array (
"test" => "TEST_3",
"datetime" => "2019-02-15 11:29:45",
)
);
// Comparison function
function dtCompare($ele1, $ele2) {
$datetime1 = strtotime($ele1['datetime']);
$datetime2 = strtotime($ele2['datetime']);
return $datetime1 - $datetime2;
}
// Sort the array
usort($array, 'dtCompare');
// Print the array
print_r($array)
?>
//Output
Array
(
[0] => Array
(
[test] => TEST_2
[datetime] => 2019-02-13 11:29:45
)
[1] => Array
(
[test] => TEST_3
[datetime] => 2019-02-15 11:29:45
)
[2] => Array
(
[test] => TEST_1
[datetime] => 2019-02-22 11:29:45
)
)