【问题标题】:Compare Time: It is 7am, what time is closer 5am or 10am?比较时间:现在是早上 7 点,早上 5 点或 10 点更接近什么时间?
【发布时间】:2012-10-12 06:10:27
【问题描述】:

我正在尝试比较时间,但我不完全确定处理此问题的最佳方法。

我有一系列无法​​编辑的时间。 数组(“500”、“1100”、“1700”、“2300”);

500 = 凌晨 5:00 等...

如果是早上 6 点或 7 点,我可以运行什么样的逻辑来确定是早上 7 点,早上 5 点或 10 点更接近什么时间?

我不认为这很复杂,但我只是想找出一个体面的解决方案,而不是我试图一起破解一些东西。

任何帮助或指导将不胜感激!

【问题讨论】:

  • 对于"0550", "0615" 的值,数学变得更有趣 - 更接近 6:00?我建议首先标准化为小数单位 - 例如5:50 是 5.83 - 如果它只需要看看哪个更近..

标签: php date time compare


【解决方案1】:

让我们从你拥有的数组开始:

$values = array("500", "1100", "1700", "2300");

我们想要的是将其格式化为有效的时间字符串,这很简单,我们只需在正确的位置插入“:”即可。为此我创建了一个函数:

function Format($str)
{
    $length = strlen($str);
    return substr($str, 0, $length - 2).':'.substr($str, $length - 2);
}

现在我们可以使用strtotime 获得可以转换为unix 时间的有效字符串。现在的问题是找到更接近当前时间的时间(我们用time 得到)

因此,我们可以遍历数组,转换它们,计算与当前时间的差值(绝对值),然后选择产生较小数字的那个。代码如下:

$now = time(); //current time
$best = false;
$bestDiff = 0;
for ($index = 0; $index < count($values); $index++)
{
    $diff = abs($now - strtotime(Format($values[$index])));
    if ($best === false || $diff < $bestDiff)
    {
        $best = $index;
        $bestDiff = $diff;
    }
}

它将在$best 中留下更接近时间的索引,在$bestDiff 中留下与计算时刻的差异。请注意,这一切都是假设这些时间是同一天和当地时间。

【讨论】:

    【解决方案2】:

    我调整了 Theraot 的解决方案,按值与当前时间的距离对数组进行排序:

    <?php
    $values = array("500", "1100", "1700", "2300");
    $now = time();
    
    /**
     *  Format an integer-string to a date-string
     */
    function format($str)
    {
        $length = strlen($str);
        return substr($str, 0, $length - 2).':'.substr($str, $length - 2);
    }
    
    /**
     * Callback to compare the distance to now for two entries in $values
     */
    $compare = function ($timeA, $timeB) use ($now) {
        $diffA = abs($now - strtotime(format($timeA)));
        $diffB = abs($now - strtotime(format($timeB)));
        if ($diffA == $diffB) {
            return 0;
        }
        return ($diffA < $diffB) ? -1 : 1;
    };
    
    usort($values, $compare);
    
    print_r($values);
    

    您想要的结果现在在 $values[0] 中。 请注意,此解决方案需要 php 版本 >= 5.3

    【讨论】:

      【解决方案3】:

      两次之差的绝对值

      比如说 07:00 - 05:00 = 02:00,02:00 的绝对值仍然是 02:00

      07:00 - 10:00 = -03:00,-03:00 的绝对值为 03:00

      在 PHP 中,您可以使用 strtotime 将时间字符串转换为秒:

      $time_one = strtotime("07:00");
      $time_two = strtotime("05:00");
      $time_three = strtotime("09:00");
      

      【讨论】:

      • 但是“0700”-“0500”怎么办?
      • 每当您使用 strtotime() - php.net/manual/en/function.strtotime.php 时,您都会在自纪元以来以秒为单位返回的字符串中提及您的时间。这是一个可用于计算的数字
      【解决方案4】:

      这是我的解决方案:

      // Input data
      $values = array("500", "1100", "1700", "2300");
      $time = "12:15";
      
      // turns search time to timestamp
      $search = strtotime($time);
      
      // turns "500" to "5:00" and so on
      $new = preg_replace('/^(\d?\d)(\d\d)$/','\1:\2', $values);
      
      // converts the array to timestamp
      $new = array_map('strtotime', $new);
      
      // sorts the array (just in case)
      asort($new);
      
      // Some initialization
      $distance = $closest = $result = $result_index = NULL;
      
      // The search itself
      foreach($new as $idx => $time_stamp)
      {
              $distance = abs($time_stamp - $search);
              if(is_null($closest) OR $closest > $distance)
              {
                      $closest = $distance;
                      $result_index = $idx;
                      $result = $time_stamp;
      
              }
      }
      echo "The closest to $time is ".date('H:i', $result)." ({$values[$result_index]})";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-21
        • 2018-01-02
        • 2013-03-08
        • 2018-01-06
        相关资源
        最近更新 更多