【发布时间】:2019-08-16 12:50:42
【问题描述】:
我有一个 JSON 源,我正在尝试循环遍历它并显示一些结果(最多 9 个结果),这不是问题
问题是我只想显示与某个日期匹配的结果,其中日期可能准确或介于两个日期之间。
例如,我只想显示日期 2019-11-17 在事件的 timeFrom timeTo 或 timeFrom 内的事件strong> 或 timeTo 等于它。在该示例中,它将是事件 1 和 3
这是源样本
{
"title":"event 1",
"timeFrom":"2019-11-16 19:00:00",
"timeTo":"2019-11-18 22:00:00",
"listText":"text of the event",
"url":"https://url",
"imageUrl":"https://image.jpg",
"locations":{
"title":"Location name",
"url":"https://location"
}
},
{
"title":"event 2",
"timeFrom":"2019-11-20 19:00:00",
"timeTo":"2019-11-20 22:00:00",
"listText":"text of the event",
"url":"https://url",
"imageUrl":"https://image.jpg",
"locations":{
"title":"Location name",
"url":"https://location"
}
},
{
"title":"event 3",
"timeFrom":"2019-11-17 19:00:00",
"timeTo":"2019-11-17 22:00:00",
"listText":"text of the event",
"url":"https://url",
"imageUrl":"https://image.jpg",
"locations":{
"title":"Location name",
"url":"https://location"
}
这是我目前拥有的 foreach
foreach(array_slice($arr, 0, 9) as $data) {
//then I will show the result
}
所以,我不知道如何在 foreach 中创建该条件。
【问题讨论】:
-
您可以将所有日期转换为时间戳,然后检查 timeFrom 是否小于您提供的时间戳和 timeTo 是否大于您提供的时间戳
-
不确定这是否适用于事件 3,因为时间戳会小于事件 3 的 timeFrom,对吧?