Php 为未来日期添加当前类

Php 为未来日期添加当前类,php,Php,我有一个从不同日期开始的事件列表 2015年12月4日至2016年1月3日 2016年1月8日至2016年2月14日 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(

我有一个从不同日期开始的事件列表

  • 2015年12月4日至2016年1月3日
  • 2016年1月8日至2016年2月14日
  • 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 '';
    }
    
    $startDate=get\u post\u meta(“事件开始日期”,true);
    $endDate=get_post_meta(“事件结束日期”),true;
    $currentDate=当前时间('mysql');
    如果(($currentDate>$startDate)&($currentDate<$endDate)){
    返回“当前事件”;
    }否则{
    返回“”;
    }
    
    这一切都很好,并完成了它的工作,但我遇到的是,两个事件之间没有任何日期可供比较。例如,如果今天的日期是1月5日,而下一个事件从1月8日开始,则不会将当前事件类添加到未来事件中。我想我必须在代码中添加另一个elseif语句,但是我应该用什么来比较它呢


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

    我想这就是您想要的:

    if ( ($currentDate > $startDate) && ($currentDate < $endDate) ) {
        return 'current-event';
    } else if($currentDate < $startDate) {
        return 'current-event';
    } else  {
        return '';
    }
    
    if($currentDate>$startDate)&($currentDate<$endDate)){
    返回“当前事件”;
    }else if($currentDate<$startDate){
    返回“当前事件”;
    }否则{
    返回“”;
    }
    
    //第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 '';    
            }
        }
    } 
    
    $startDate=get\u post\u meta(“事件开始日期”,true);
    $endDate=get_post_meta(“事件结束日期”),true;
    $currentDate=当前时间('mysql');
    $startDays=array()//将开始日期分配给此数组并对其进行排序
    如果(($currentDate>$startDate)&($currentDate<$endDate)){
    返回“当前事件”;
    }否则{
    foreach($startDays作为$day)
    {
    如果($currentDate<$day)
    {
    /*
    这将返回下一个事件的当前事件(而不是其他下一个事件)。
    但请确保对开始日期数组中的日期进行排序。
    */
    返回“当前事件”;
    打破
    }否则
    {
    返回“”;
    }
    }
    } 
    
    差不多,但问题是它还将当前事件类添加到所有未来事件中。我没有想到我只想把这个类添加到一个单独的未来事件中。我想了一个方法,看看它是否适合你。