Php 与当前时间比较时,在数组中搜索时间

Php 与当前时间比较时,在数组中搜索时间,php,arrays,Php,Arrays,我将时间列表存储在数组中。我想搜索时间,看看数组中的时间是否接近我的当前时间 示例:我的当前时间是01:16,因此在数组中有01.00、01:30、02:00和05:00。如果我的当前时间显示为01:16或大于,则最接近的时间将为01:00,因此我希望获得整数值,即3。如果我的当前时间显示为01:30或大于数组中的时间01:30,则正确的时间应为01:30,因此我希望获得值3。如果我的当前时间显示为02:00或大于数组中的时间02:00,则正确的时间应为02:00,因此我希望获得值4。同样适用于

我将时间列表存储在数组中。我想搜索时间,看看数组中的时间是否接近我的当前时间

示例:我的当前时间是
01:16
,因此在数组中有
01.00
01:30
02:00
05:00
。如果我的当前时间显示为
01:16
或大于,则最接近的时间将为
01:00
,因此我希望获得整数值,即
3
。如果我的当前时间显示为
01:30
或大于数组中的时间
01:30
,则正确的时间应为
01:30
,因此我希望获得值
3
。如果我的当前时间显示为
02:00
或大于数组中的时间
02:00
,则正确的时间应为
02:00
,因此我希望获得值
4
。同样适用于
05.00

代码如下:

function get_shows($day,$channel_id, DateTime $dt, $today = false) 
{

   $ch = curl_init();
   curl_setopt_array($ch, array(
      CURLOPT_USERAGENT => '',
      CURLOPT_TIMEOUT => 30,
      CURLOPT_CONNECTTIMEOUT => 30,
      CURLOPT_HEADER => false,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_MAXREDIRS => 5,
      CURLOPT_SSL_VERIFYPEER => false
   ));

   $date = $dt->format('Y-m-d');
   $tz = $dt->getTimezone();

   $now = new DateTime('now', $tz);
   $today = $now->format('Y-m-d');
   $shows = array();
   $url = 'https://www.example.com?date=' . $date;
   curl_setopt($ch, CURLOPT_URL, $url);
   $body = curl_exec($ch); //get the page contents
   $channel_row = $row_channels[0][0]; // Woksepp: 0 = First row.
   $pattern23 = "/<a class=\"prog\" href=\"(.*?)\">.*?<span class=\"time\">(.*?)<\/span>.*?<span class=\"title\" href=\"\#\">(.*?)<\/span>.*?<span class=\"desc\">(.*?)<\/span>/s";
    preg_match_all($pattern23, $channel_row, $d);
   $show_times = $d[2];

   if($day==0)
   {
      //check if my current time is close to the time in the arrays then set the $flag value
     //$flag = $i
   }
}
?>
我希望做的是检查数组中的时间是否接近当前时间,因此我希望获得整数值,以便使用如下值设置
$flag

你能给我举个例子吗?当数组中的时间接近时,我如何将数组中的时间与当前时间进行比较,以便得到整数值?

PHP有一个漂亮的函数,可以让你将字符串转换为unix时间戳,这使比较两次变得非常容易

然后,您只需迭代数组,找到差异最小的时间(当前时间的绝对值减去数组中的时间),并将该特定时间的数组键保存在变量中

$currentTime  = time();
$minTimeValue = PHP_INT_MAX;
$minTimeKey   = -1;

foreach ($array as $key => $time) {
    $thisTimeDifference = abs($currentTime - strtotime($time));
    if ($thisTimeDifference < $minTimeValue) {
        $minTimeKey = $key;
        $minTimeValue = $thisTimeDifference;
    }
}
$currentTime=time();
$minTimeValue=PHP\u INT\u MAX;
$minTimeKey=-1;
foreach($key=>$time的数组){
$thistTimeDifference=abs($currentTime-strotTime($time));
如果($ThistTimeDifference<$minTimeValue){
$minTimeKey=$key;
$minTimeValue=$ThistTimeDifference;
}
}
PHP有一个漂亮的函数,可以将字符串转换为unix时间戳,这样比较两次就容易多了

然后,您只需迭代数组,找到差异最小的时间(当前时间的绝对值减去数组中的时间),并将该特定时间的数组键保存在变量中

$currentTime  = time();
$minTimeValue = PHP_INT_MAX;
$minTimeKey   = -1;

foreach ($array as $key => $time) {
    $thisTimeDifference = abs($currentTime - strtotime($time));
    if ($thisTimeDifference < $minTimeValue) {
        $minTimeKey = $key;
        $minTimeValue = $thisTimeDifference;
    }
}
$currentTime=time();
$minTimeValue=PHP\u INT\u MAX;
$minTimeKey=-1;
foreach($key=>$time的数组){
$thistTimeDifference=abs($currentTime-strotTime($time));
如果($ThistTimeDifference<$minTimeValue){
$minTimeKey=$key;
$minTimeValue=$ThistTimeDifference;
}
}

“那里的数组有
01.00
01:30
02:00
05:00
。如果我的当前时间显示
01:16
或更大,最近的时间将是
01:00
”-1:30接近1:16(相隔14分钟),而不是1:00(16分钟)。“那里的数组有
01.00
01:30
02:00
05:00
。如果我的当前时间显示为
01:16
或大于,则最近的时间应为
01:00
“-1:30关闭至1:16(相隔14分钟),而不是1:00(16分钟)。谢谢,那么当我的当前时间等于或大于数组中的时间时,如何使用
$flag
设置值?谢谢,那么当我的当前时间等于或大于数组中的时间时,如何使用
$flag
设置值?