查找日期时间数组中的下一个事件

问题描述:

我有以下由日期时间和事件组成的数组。我期待从今天的日期返回下一个事件。例如:如果今天的日期是2010-11-19,则返回“足球比赛”。查找日期时间数组中的下一个事件

达到这个结果的最佳方法是什么?

非常感谢。

Array(
    [1] => Array 
     (
      [start] => 20101113T100000Z 
      [event] => Fishing at the Pond 
     ) 

    ... etc ... 

    [29] => Array 
     (
      [start] => 20101125T150000Z 
      [event] => Football Match 
     ) 
) 
+0

而不是阵列的所有位置都是事件吗?你如何区分事件与其他事物? – acm 2010-11-19 10:32:59

$date = '2010-11-19'; 

$next = $array[0]; 
foreach ($array as $item) 
    if (strtotime($date) < strtotime($item['start']) && strtotime($date) > strtotime($next['start'])) $next = $item; 

echo $next['event']; 
+0

谢谢阿克塞尔隆,我知道有一个简单的答案 – 2010-11-22 17:12:21

为什么不把datetime作为数组的键?伊莫这会更简单,因为你只有2个值。

Array(
[20101113T100000Z] => Fishing at the Pond 
[20101125T150000Z] => Football Match 
) 

然后使用foreach,测试每个值与今天的日期,并停止当你找到你想要的。