【问题标题】:Find closest next hour in array of hours [closed]在小时数数组中查找最近的下一小时[关闭]
【发布时间】:2020-10-10 15:56:30
【问题描述】:

我想在一个小时数组中找到下一个最近的小时,我有下面的代码可以在前后两个方向上搜索最近的小时。

我需要做的是只搜索下一个小时,在下面的示例中应该是 14:00 而不是 03:50。

条件详情:

  • 持续时间:24H - 1D
  • 开始时间:00:00
  • $timeslots 和 $expected_time 中的值都是动态的
$timeslots = ["01:00","01:30","02:00","02:30","00:30","01:00","01:30","02:00","02:30","03:50", "14:00"];
$expected_time = "04:00";
$timestamp = strtotime($expected_time);
$diff = null;
$index = null;
        foreach ($timeslots as $key => $time) {
            $currDiff = abs($timestamp - strtotime($time));
           if (is_null($diff) || $currDiff < $diff) {
                $index = $key;
                $diff = $currDiff;
            }
        }
echo $timeslots[$index];

【问题讨论】:

  • 你想找到什么?基于什么参数?你能提供更多的例子吗?
  • 不清楚 1)为什么$timeslots 数组没有排序; 2)如果最近的 next 插槽无论如何都应该被占用,为什么有人试图计算两次之间的模差。
  • 你没有指定最接近对你意味着什么。

标签: php arrays time


【解决方案1】:

在开始迭代之前对数组进行排序总是一个好主意。

然后,一旦我们在数组中找到匹配项,我们就会中断循环并输出我们找到的内容

$timeslots = ["01:00","01:30","02:00","02:30","00:30","01:00","01:30","02:00","02:30","03:50", "14:00", "15:00", "15:30"];
sort($timeslots); // first we sort the array, very important

$expected_time = "04:00";
$timestamp = strtotime($expected_time);
$diff = PHP_INT_MAX;
$index = null;

foreach ($timeslots as $key => $time) {
    
    $currDiff = $timestamp - strtotime($time);

    // Add a condition to check if the difference was negative or not - because it'll only be the "next" hour when the difference is negative
    if ($currDiff < $diff && $currDiff < 0) {
        $index = $key;
        $diff = $currDiff;
        break;
    }
}

if(!is_null($index))
    echo $timeslots[$index];
else
    echo 'Not found';

https://3v4l.org/kGdP8

【讨论】:

  • 您能否解释一下这与我上面的代码产生的结果有何不同? 3v4l.org/EQ6h1
  • 在那种特定情况下没有区别。但是当你在 14:00 之后有更多的时间时,它的工作方式就不同了
  • 啊,我现在明白了 - 您在小时数组 15:30 中添加了另一个元素,这时它开始给出不正确的结果。之所以发生这种情况,是因为负数是反向比较的。所以currDiff 总是小于 $diff。
  • 我现在已经在我的答案中修复了它..
  • 虽然这是假设数组可能不同并且输入可能不同。这是一种健康的假设方式
【解决方案2】:

您的代码似乎正确。它不会返回 下一个 小时,因为您使用时差的绝对值进行比较,而是这样做:

$timeslots = ["01:00","01:30","02:00","02:30","00:30","01:00","01:30","02:00","02:30","03:50", "14:00"];

$expected_time = "04:00";
$timestamp = strtotime($expected_time);
$diff = null;
$index = null;

foreach ($timeslots as $key => $time) {
    
    // Don't use abs function here - to get negative values as well
    $currDiff = $timestamp - strtotime($time);

    // Add a condition to check if the difference was negative or not - because it'll only be the "next" hour when the difference is negative
    if ((is_null($diff) || abs($currDiff) < $diff) && $currDiff < 0) {
        $index = $key;
        $diff = abs($currDiff);
    }
}

if(!is_null($index))
    echo $timeslots[$index];
else
    echo 'Not found';

【讨论】:

  • 找不到18:00的值
  • 下一个是00:30。问题不正确,因为它与下一次最接近。
  • 我会说这不起作用。您不妨只使用 end($arr)。 3v4l.org/NeOTE
  • 我没有说你的答案不正确,但你表明你不理解这个问题。为什么你没有返回3:50?这是下一个最近的时间。
  • 这也是你的代码所做的。看链接
猜你喜欢
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 2015-07-30
  • 1970-01-01
相关资源
最近更新 更多