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

发布于 2024-10-03 12:05:51 字数 423 浏览 0 评论 0原文

我有以下数组,由日期时间和事件组成。我希望返回今天的下一个活动。例如:如果今天的日期是 2010 年 11 月 19 日,则返回“足球比赛”。

实现这一结果的最佳方法是什么?

非常感谢。

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

    ... etc ...

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

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] => 20101113T100000Z
            [event] => Fishing at the Pond
        )

    ... etc ...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天气好吗我好吗 2024-10-10 12:05:51

为什么不将日期时间作为数组的键?我认为这会简单得多,因为你只有 2 个值。

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

然后使用 foreach,用今天的日期测试每个值,并在找到所需内容时停止。

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.

盗琴音 2024-10-10 12:05:51
$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'];
$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'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文