Support

Account

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

Solving

Extract and compare month from two date time picker fields

  • For our review site we wish to display events for the duration of the event.
    Say an event starts July 1st, ends on July 4th, a user can select
    July 1st in the acf field ‘datum’ and
    July 4th in the acf field ‘eind_datum’.

    Front end this should then look like “1-4 Juli 2020” (in the Dutch language)
    However, if an event takes place from June 30th until July 4th it should look like this “30 Juni – 4 Juli 2020”.

    In order to get this done I came up with the code below to compare the month. Is it the same, then the month should only be displayed once. Is it different, both the months should be displayed. I got this partially working, but for the life of me I can’t get the compare month function to work.

    Anybody on here that can help ?

     <?php  
    
    $start_date = get_field('datum');
    $end_date = get_field('eind_datum');
    $startdatetime = DateTime::createFromFormat("Y-m-d H:i:s", $start_date);
    $enddatetime = DateTime::createFromFormat("Y-m-d H:i:s", $end_date);
    $first = $startdateTime->format('F');
    $second = $enddateTime->format('F');
    	
     if (get_field('eind_datum') and get_field('datum') and ($first == $second )) {
      echo  $start_date->format ('j'); echo "-"; echo $end_date->format ('j F Y') ;
     }
     elseif (get_field('eind_datum') and get_field('datum') and ($first != $second )) {
      echo  $start_date->format('j F'); echo "-"; echo $end_date->format ('j F Y') ; 
     }
     else {
      echo the_field('datum') ;
     }
     ?>

    I also had another variation, but sadly wordpress throws an error for this as well.

     <?php  
    	$start_date = get_field('datum', false, false);
        $start_date = new DateTime($start_date);
        $end_date = get_field('eind_datum', false, false);
        $end_date = new DateTime($end_date);
        $start_month = $start_date->format('F');
        $end_month = $end_date->format('F');
    	
     if (get_field('eind_datum') and get_field('datum') and ($start_month == $end_month )) {
      echo  $start_date->format ('j'); echo "-"; echo $end_date->format ('j F Y') ;
     }
     elseif (get_field('eind_datum') and get_field('datum') and ($start_month != $end_month )) {
      echo  $start_date->format('j F'); echo "-"; echo $end_date->format ('j F Y') ; 
     }
     else {
      echo the_field('datum') ;
     }
     ?>
  • 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;
    
  • While this partially solves the problem (the date is now correctly displayed as I want it), all dates are shown as 1-1 January 1970.

    I know this has to do with the unix time stamp, which I encountered with a different go at cracking the code.

    Any thoughts on how to solve this ? Nearly there, I can feel it 🙂
    This is what I got at the moment

    <?php  
    $start_date = get_field('datum');
    $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('eind_datum');
    $end_time = strtotime($end_date);
    $end_year = date('Y', $end_time);
    $end_month = date('F', $end_time);
    $end_day = date('j', $end_time);
    
    if (get_field('eind_datum') and get_field('datum') and ($end_month == $start_month)) {
      $display_date = $start_day.' - '.$end_day.' '.$end_month.' '.$end_year;
    } 
    elseif (get_field('eind_datum') and get_field('datum') and ($end_month != $start_month)) {
      $display_date = $start_day.' '.$start_month.' - '.$end_day.' '.$end_month.', '.$end_year;
    }
    else {
      $display_date = $start_day.' '.$start_month.' '.$start_year;
    }
    	
    echo $display_date;
    	 ?>
  • When getting 1970 the problem is your return format. The format being returned is not an acceptable format use in date functions. When I’m going to be manipulating dates I always set the return format to “Y-m-d” or “Y-m-d H:i:s” for date time fields and then I reformat it as I need.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.