Php 在小时数组中查找最近的下一个小时

Php 在小时数组中查找最近的下一个小时,php,arrays,time,Php,Arrays,Time,我想在小时数组中找到下一个最近的小时,我有下面的代码在前后两个方向上搜索最近的小时 我需要做的是只搜索下一个小时,在下面的例子中应该是14:00而不是03:50 有关条件的详细信息: 持续时间:24小时-1天 开始时间:00:00 $timeslots和$expected\u time中的值都是动态的 您的代码似乎是正确的。它不会返回下一个小时,因为您正在使用时间差的绝对值进行比较,请执行以下操作: $timeslots = ["01:00","01:30"

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

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

有关条件的详细信息:

持续时间:24小时-1天 开始时间:00:00 $timeslots和$expected\u 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) {
    
    // 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';

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

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

$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';

你想找到什么?基于什么参数?你能提供更多的例子吗?不清楚为什么$timeslots数组没有排序;2为什么有人试图计算两次之间的模差,如果应该取最近的下一个插槽。您没有指定最近的插槽对您来说意味着什么。找不到18:00的值下一个插槽是00:30。这个问题是不正确的,因为它和下一次混在一起。我认为这不起作用。你不妨使用end$arr。我没有说你的答案不正确,但你表明你不理解这个问题。你为什么3:50不回来?这是下一个最接近tho的小时。这也是你的代码所做的。请看链接,你能解释一下这与我上面的代码不同的结果吗?在这一特定场景中,没有区别。但是当你在14:00之后有更多的时间,那么它的工作方式就不同了是的,我现在知道了-你在15:30的时间数组中添加了另一个元素,这就是它开始给出错误结果的时候。这种情况的发生是因为消极因素的对比是相反的。。所以currDiff总是小于$diff。我现在在回答中修正了它。尽管这是一个假设,数组可能不同,输入可能不同。这是一种健康的假设方式
$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';