【问题标题】:How to sort dates from closest date to the furthest in an array of dates in PHP如何在PHP中的日期数组中从最近的日期到最远的日期对日期进行排序
【发布时间】:2021-02-08 08:54:47
【问题描述】:

This post回答了这个问题,但我不是很明白答案

我有一个包含日期的数组,

我想对列date_res结果日期)按最接近今天的排序,而不是最大或最小

我的数组可以帮助你

<?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'
    ]);
?>

【问题讨论】:

  • 今天之前的日期应该如何处理?
  • @John Hanley,它会从日期变成一个链接,因为在结果的日期结束后,结果会出现并有一个链接
  • 这不是我的问题。你的问题是最接近今天的sort。在你的排序程序中,昨天和明天一样吗?
  • OP 似乎想要今天和 date_res 之间的绝对差异/距离从最小到最大排序。昨天和明天的距离相等。
  • 我不知道,但我认为如果你把 yasterday 放在同一个程序中没有问题,因为 yasterday 会被隐藏,@John Hanley

标签: php arrays sorting


【解决方案1】:

您需要比较date_res 条目与today差异

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

abs 用于获取date_restoday 之间的“距离”,无论它之前还是之后。然后你只需比较“距离”。

工作example

参考文献

【讨论】:

    【解决方案2】:

    尝试(请注意,我在sort_dates 函数中将 date_res 列转换为日期,因为它是字符串):

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

    Tested it 我使用您提供的数组获得所需的输出。

    【讨论】:

    • 我对其进行了测试,它适用于我与您提供的相同数组。
    • 你确定从这段代码中,因为我认为我的代码中的问题是我在其中添加了你的代码
    • 我换个方式试试
    • 我编辑了问题并链接到代码和输出以显示它有效。
    • 这个答案不符合标准:closest to today not the greatest or the smallest 你只是在date_res 上排序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多