开发者

Finding the next event in an array of datetimes

I have the following array which is made up of a datetime and an event. I am looking to return the next event from today's date. Eg: If today's date is 2010-11-19 then 'Football Match' is returned.

What is the best way to achieve this result?

Many thanks.

Array(
    [1] => Array
        (
            [start] =>开发者_如何学Python 20101113T100000Z
            [event] => Fishing at the Pond
        )

    ... etc ...

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


Why don't you put your datetime as your array's key ? Imo that would be much simpler, as you only have 2 values.

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

Then with a foreach, you test each value with today's date, and stop when you found what you want.


$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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜