为将来的日期添加当前类


Add current class for future date

我有一个在不同日期开始和开始的事件列表。

  1. 2015年12月4日至2016年1月3日
  2. 2016年1月8日至2016年2月14日
  3. 2016年2月26日至2016年3月27日

我有一段代码,如果某个事件在某个日期之间,它会为该事件添加一个当前事件类。

$startDate = get_post_meta('_event_start_date', true);
$endDate = get_post_meta('_event_end_date', true);
$currentDate = current_time( 'mysql' );
if ( ($currentDate > $startDate) && ($currentDate < $endDate) ) {
    return 'current-event';
} else {
    return '';
}

这一切都很好,也很有效,但我遇到的是,在两个事件之间没有任何日期可以比较。例如,如果今天的日期是1月5日,而下一个事件从1月8日开始,则不会将当前事件类添加到未来的事件中。我想我必须在代码中添加另一个elseif语句,但我应该将其与什么进行比较?

在这里,我只想为未来的一个事件添加当前事件类。

我想这就是你想要的:

if ( ($currentDate > $startDate) && ($currentDate < $endDate) ) {
    return 'current-event';
} else if($currentDate < $startDate) {
    return 'current-event';
} else  {
    return '';
}

//版本2:

$startDate = get_post_meta('_event_start_date', true);
$endDate = get_post_meta('_event_end_date', true);
$currentDate = current_time( 'mysql' );
$startDays = array();      //assign your start days to this array & sort it as well
if ( ($currentDate > $startDate) && ($currentDate < $endDate) ) {
    return 'current-event';
} else {
    foreach($startDays as $day)
    {
        if($currentDate < $day)
        {
           /*
           this will return current event for the exactly next event (and not for the other next events).
           but make sure to sort the dates in the start days array.
           */
            return 'current-event';         
            break;
        }else
        {
            return '';    
        }
    }
}