PHP获得最接近给定时间的时间

时间:2018-11-13 15:26:42

标签: php arrays datetime difference php-carbon

我有一个名为$timeslots的数组,其时隙类似于:

array:32 [▼
  0 => "2018-12-15T12:00:00.0000000"
  1 => "2018-12-15T12:15:00.0000000"
  2 => "2018-12-15T12:30:00.0000000"
  3 => "2018-12-15T12:45:00.0000000"
  4 => "2018-12-15T13:00:00.0000000"
  5 => "2018-12-15T13:15:00.0000000"
  6 => "2018-12-15T13:45:00.0000000"
  7 => "2018-12-15T14:15:00.0000000"
  8 => "2018-12-15T14:30:00.0000000"
  9 => "2018-12-15T14:45:00.0000000"
  10 => "2018-12-15T15:00:00.0000000"
  11 => "2018-12-15T15:15:00.0000000"
  12 => "2018-12-15T15:30:00.0000000"
  13 => "2018-12-15T15:45:00.0000000"
  14 => "2018-12-15T16:15:00.0000000"
  15 => "2018-12-15T16:45:00.0000000"
  16 => "2018-12-15T17:00:00.0000000"
  17 => "2018-12-15T17:30:00.0000000"
  18 => "2018-12-15T17:45:00.0000000"
  19 => "2018-12-15T18:30:00.0000000"
  20 => "2018-12-15T18:45:00.0000000"
  21 => "2018-12-15T19:15:00.0000000"
  22 => "2018-12-15T19:45:00.0000000"
  23 => "2018-12-15T20:15:00.0000000"
  24 => "2018-12-15T20:45:00.0000000"
  25 => "2018-12-15T21:00:00.0000000"
  26 => "2018-12-15T21:15:00.0000000"
  27 => "2018-12-15T21:30:00.0000000"
  28 => "2018-12-15T21:45:00.0000000"
  29 => "2018-12-15T22:00:00.0000000"
  30 => "2018-12-15T22:15:00.0000000"
  31 => "2018-12-15T22:30:00.0000000"
]

此外,我有一个变量,如:

$expected_time = 2018-12-15T18:00:00.0000000; // this can be different value, so its not unique value

$ expected_time永远不会进入数组$timeslots中,但是我需要找到最接近$ expected_time的值...我该怎么做?

如何获取从数组$timeslots$expected_time的最接近的时隙值,并计算分钟差?

有什么主意吗?

3 个答案:

答案 0 :(得分:2)

正如Nico在评论中提到的那样,这非常简单。只是循环并计算时差。

$timeslots = [...];
$expected_time = "2018-12-15T18:00:00.0000000";
$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 :(得分:1)

这是一种符合您要求的解决方案:

function findClosestDate($expectedDate,$dates)
{

    $differenceInMinutes = null; 
    $expectedDate = new DateTime($expectedDate);
    $expectedDateEpoch = $expectedDate->getTimestamp();
    $returnIndex = -1;

    for($i = 0; $i<count($dates); $i++)
    {
        $dateObject = new DateTime($dates[$i]);
        $dateEpoch = $dateObject->getTimestamp();
        $difference = abs($expectedDateEpoch-$dateEpoch);
        $difference = $difference/60;
        if($differenceInMinutes === null || $difference < $differenceInMinutes)
        {
            $differenceInMinutes = $difference;
            $returnIndex = $i;
        }
    }

    return array(
        "closest" => $dates[$returnIndex],
        "difference" => $differenceInMinutes
    ) ;
}

这利用DateTime类来创建DateTime对象并获得相应的timestamp。然后根据expectedDatedates数组中的条目之间的绝对差来计算分钟。遍历整个数组后,最接近的匹配项和差值将在一个数组中返回。

答案 2 :(得分:1)

,因为您的列表已经排序-您可以将元素放入数组中,然后再次使用sort-它将比在每次迭代中计算diff更快。

<?php
$timeslots = [
    ...
];

$expected_time = "2018-12-15T18:00:00.0000000";
$counter = count($timeslots);
$timeslots = array_flip($timeslots);
$timeslots[$expected_time] = $counter;
ksort($timeslots);

while (key($timeslots) !== $expected_time) {
    $prev = key($timeslots);
    next($timeslots);
}

next($timeslots);
$next = key($timeslots);

$expected_time = new \DateTime($expected_time);
$closestDiff = min(($expected_time)->diff(new \DateTime($prev)), (new \DateTime($next))->diff($expected_time));
var_dump($closestDiff->i);