Support

Account

Home Forums General Issues Extract and compare month from two date time picker fields Reply To: Extract and compare month from two date time picker fields

  • I’ve recently needed to do something similar, also don’t forget that you could have something that run from December of one year to January of the following year.

    I don’t use DateTime because I feel it just over complicates things, but here is the code I used for that site.

    
    $this_year = date('Y');
    
    $start_date = get_field('start_date');
    $start_time = strtotime($start_date);
    $start_year = date('Y', $start_time);
    $start_month = date('F', $start_time);
    $start_day = date('j', $start_time);
    
    $end_date = get_field('end_date');
    $end_time = strtotime($end_date);
    $end_year = date('Y', $end_time);
    $end_month = date('F', $end_time);
    $end_day = date('j', $end_time);
    
    if ($end_year != $start_year) {
      $display_date = $start_month.' '.$start_day.', '.$start_year.' - '.$end_month.' '.$end_day.', '.$end_year;
    } elseif ($end_month != $start_month) {
      $display_date = $start_month.' '.$start_day.' - '.$end_month.' '.$end_day;
      if ($start_year != $this_year) {
        $display_date .= ', '.$start_year;
      }
    } elseif ($end_day != $start_day) {
      $display_date = $start_month.' '.$start_day.' - '.$end_day;
      if ($start_year != $this_year) {
        $display_date .= ', '.$start_year;
      }
    } else {
      $display_date = $start_month.' '.$start_day;
      if ($start_year != $this_year) {
        $display_date .= ', '.$start_year;
      }
    }
    echo $display_date;