0

This post answered this question but I didn't understand the answers very well

I have an array contains dates,

I want to sort column date_res (results date) by the closest to today not the greatest or the smallest

my array may help you

<?php 
$today = date('Y-m-d');
$arr = array(
    [
        'id' => '1',
        'date' => '2020-03-27',
        'date_end' => '2020-05-02 17:00',
        'date_res' => '2020-06-02'
    ],
    [
        'id' => '2',
        'date' => '2020-04-27',
        'date_end' => '2020-06-02 17:00',
        'date_res' => '2020-08-02'
    ],
    [
        'id' => '3',
        'date' => '2020-06-27',
        'date_end' => '2020-07-02 17:00',
        'date_res' => '2020-08-02'
    ],
    [
        'id' => '4',
        'date' => '2021-01-01',
        'date_end' => '2021-02-01 17:00',
        'date_res' => '2021-03-01'
    ],
    [
        'id' => '5',
        'date' => '2021-02-01',
        'date_end' => '2021-03-01 17:00',
        'date_res' => '2021-04-01'
    ],
    [
        'id' => '6',
        'date' => '2021-05-01',
        'date_end' => '2021-06-01 17:00',
        'date_res' => '2021-08-01'
    ],
    [
        'id' => '7',
        'date' => '2021-02-06',
        'date_end' => '2021-01-29 17:00',
        'date_res' => '2021-01-29'
    ],
    [
        'id' => '9',
        'date' => '2021-04-27',
        'date_end' => '2021-05-03 17:00',
        'date_res' => '2021-05-03'
    ],
    [
        'id' => '10',
        'date' => '2021-02-01',
        'date_end' => '2021-02-06 17:00',
        'date_res' => '2021-05-03'
    ],
    [
        'id' => '11',
        'date' => '2021-02-02',
        'date_end' => '2021-02-07 17:00',
        'date_res' => '2021-05-05'
    ],
    [
        'id' => '12',
        'date' => '2021-03-27',
        'date_end' => '2021-02-07 17:00',
        'date_res' => '2021-06-02'
    ]);
?>
Eymen
  • 27
  • 5
  • How should dates before today be handled? – John Hanley Feb 08 '21 at 09:07
  • @John Hanley, it will be changed from date to a link , Because after date of the result ends , the results will appear and have a link – Eymen Feb 08 '21 at 09:23
  • That is not my question. Your question states `sort` by closest to today. Is yesterday the same as tomorrow in your sort routine? – John Hanley Feb 08 '21 at 09:27
  • OP seems to want the absolute difference/distance between today and date_res ordered from smallest to largest. With yesterday and tomorrow being equal distance apart. – Remy Feb 08 '21 at 09:32
  • I don't know but I think if you but yasterday in the same routine there is no problem , beacause yasterday will be hidden , @John Hanley – Eymen Feb 08 '21 at 11:02
  • What shall be the output (structure)? – ominug Feb 08 '21 at 17:41

2 Answers2

1

Try (note that I converted the date_res column to date in sort_dates function as it was string):

function sort_dates($x, $y) {
    return strtotime($x['date_res']) - strtotime($y['date_res']);
}
usort($arr, "sort_dates");

Tested it and I get the desired output using the array you provided.

Silidrone
  • 1,471
  • 4
  • 20
  • 35
1

You need to compare the differences of the date_res entries to today:

usort($arr, function($dateA, $dateB) use ($today) {
    return
        abs(strtotime($dateA['date_res']) - strtotime($today)) -
        abs(strtotime($dateB['date_res']) - strtotime($today));
});

The abs is used to get the "distance" between date_res and today regardless if its before or after. Then you just compare the "distances".

Working example.

References

SirPilan
  • 4,649
  • 2
  • 13
  • 26