У меня есть сайт, который отображает события; Каждое событие может иметь х количество дат (без ограничений).
Мне нужно отобразить на главной странице событие, наиболее близкое к сегодняшней дате.
Это должно быть легко, но по какой-то причине это не работает. Любая помощь будет оценена.
Итак, вот моя функция, которая выбирает главное событие. События были предварительно извлечены из БД со связанными датами, хранящимися под ключом «даты». Они передаются функции через аргумент «$ events».
Каждая дата указывается как (int) unix date.
function getMainEvent(&$events){
$today = time();
$currentEvent = end($events); //selecting the last entered event (most likely to be the main event)
$currentEventLast = max($currentEvent['dates']); //selecting the highest date linked to that event
foreach($events as $k=>&$event){ //looping through events
$date = min($event['dates']); //selecting the lowest date for the current event
if($date>$today && $date<$currentEventLast){
//if $date is higher than today (so it's not in the past), but lower than main event's date...
$currentEvent = &$event; //make that event the main event
$currentEventLast = max($currentEvent['dates']); //select the latest date of that event
}
}
return $currentEvent;
}