函数检查时间,并根据该时间运行程序


Function to check the time and run program according to that time.

我正在为估计时间编写函数。我会根据时间运行一个特定的事件……假设我有一个数组的时间

$times = array(
    '00:00' => 'Programme 1',
    '00:30' => 'Programme 2',
    '01:00' => 'Programme 3',
    '01:30' => 'Programme 4',
    '02:00' => 'Programme 5',
    '02:30' => 'Programme 6',
    '03:00' => 'Programme 7',
    '03:30' => 'Programme 8',
    '04:00' => 'Programme 9',
    '04:30' => 'Programme 10',
    '05:00' => 'Programme 10',
);

和函数

echo (currently_should_play("00:05", $times) === 'Programme 1' ? 'Ok 1' : 'Not Ok 1') . PHP_EOL;

如果第一次小于第二次,它应该匹配第一次,如果它等于确切的值,它应该运行,甚至…

上面的函数调用应该匹配00:00并打印'ok'

让我们假设另一个例子

echo (currently_should_play("00:58", $time) === 'Programme 2' ? 'Ok 2' : 'Not Ok 2') . PHP_EOL;

它应该与第二个值匹配,因为它与程序2匹配…

请建议一些解决方案。

我的函数代码在这里,我从数组

获得键和值
function currently_should_play($current_time, array $times) {
   foreach ($times as $time => $programme) {
    echo $time;
    echo $programme."<br/>";
   }
}
function currently_should_play($current_time, array $times) {
  $s=size($times);
  while($s-->0){
   if($times[$s]==$current_time){
    return $times[$s];
   }elseif($times[$s-1]==$current_time){
    return $times[$s-1];
   }elseif($times[$s-1]<$current_time&&$times[$s]>$current_time){
    return $times[$s-1];
   }
 }
 return false;
}