条件显示开始和结束日期(PHP / Wordpress)

时间:2014-05-19 20:40:07

标签: wordpress date

(正如你在这篇文章中所说,我不是程序员,所以请帮助我看看这是否可能......)

我想改进事件管理器(WordPress插件)中事件的开始/结束日期显示

目前结束时间只会显示与开始日期不同(好!),但格式如下: 2014年1月1日至2014年1月31日。

我希望它显示为:2014年1月1日至31日。 这不是太难,但当事件包裹或延续一个月或一年时,它会变得有点复杂。

以下是我想要实现的不同案例:

仅限开始日期:

1 January 2014

同月的开始和结束日期

1 – 12 January 2014

不同月份但同年的开始和结束日期。

28 January – 3 February 2014

不同月份和不同年份的开始和结束日期。

31 December 2014 – 3 January 2015

任何人都可以告诉我

a)如果这完全可以与WordPress函数文件中的函数有关吗?

b)指出我如何进行调理的正确方向?

我甚至不知道谷歌的用途,所以即使这样也会有所帮助。 (我不期待一个完全正常工作的代码示例,但如果您愿意,我们欢迎您加入其中!)

更新:这是一个函数的例子,它执行了一些功能,但仅限于上面列表中最后一个案例的样式。

add_filter('em_event_output_placeholder','my_em_placeholder_mod_eventdates',1,3);
    function my_em_placeholder_mod_eventdates($replace, $EM_Event, $result){
            if ( $result == '#_EVENTDATES' ) {
                    if( $EM_Event->event_start_date != $EM_Event->event_end_date){
                            $replace = date_i18n('d M Y', $EM_Event->start).' - '. date_i18n('d M Y', $EM_Event->end);
                    }else{
                            $replace = date_i18n('d M Y', $EM_Event->start);
                    }
            }
            return $replace;
    }

更新2,工作代码 这段代码至少对我有用。

add_filter('em_event_output_placeholder','my_em_placeholder_mod_eventdates',1,3);
function my_em_placeholder_mod_eventdates($replace, $EM_Event, $result){
if ( $result == '#_EVENTDATES' ) :
  $sd = $EM_Event->start;
  $ed = $EM_Event->end;
  if (!empty ($ed) && $sd != $ed ) {
    //Event has an end date and end date is different than start date
    if (date('Y', $sd) == date('Y', $ed)) {
      // Start and end are in same year
      if (date('n', $sd) == date('n', $ed)) { 
        //Start and end are in the same month
        $replace = date_i18n('j', $sd).'-'.date_i18n('j F Y', $ed);
          if (date('j', $sd) == date('j', $ed)) {
            //Start and end are on the same day
            $replace = date_i18n('j F Y', $sd);
            }
      } else {
        //Start and end are in different months
        $replace = date_i18n('j F', $sd).' - '.date_i18n('j F Y', $ed);
      }
    } else {
      //Start and end are in different years
      $replace = date_i18n('j F Y', $sd).' - '. date_i18n('j F Y', $ed);
    }
  } else {
    // No end date, or start and end date are the same
    $replace = date_i18n('j F Y', $sd);
  }
endif;
return $replace;
}

1 个答案:

答案 0 :(得分:0)

试试这个:

add_filter('em_event_output_placeholder','my_em_placeholder_mod_eventdates',1,3);
function my_em_placeholder_mod_eventdates($replace, $EM_Event, $result){
    if ( $result == '#_EVENTDATES' ) :
      $sd = $EM_Event->event_start_date;
      $ed = $EM_Event->event_end_date;
      if (!empty ($ed) && $sd != $ed ) {
        //Event has an end date and end date is different than start date
        if (date('Y', $sd) == date('Y'. $ed) {
          // Start and end are in same year
          if (date('n', $sd) == date('n', $ed) {
            //Start and end are in the same month
            $replace = date_i18n('j', $sd).'-'.date_i18n('j F Y', $ed);
          } else {
            //Start and end are in different months
            $replace = date_i18n('j F', $sd).' - '.date_i18n('j F Y', $ed);
          }
        } else {
          //Start and end are in different years
          $replace = date_i18n('j F Y', $sd).' - '. date_i18n('j F Y', $ed);
        }
      } else {
        // No end date, or start and end date are the same
        $replace = date_i18n('j F Y', $sd);
      }
    endif;
    return $replace;
}