1

I've created two custom datepicker fields with ACF (beginning_date and completion_date) and need to output/display these as a date range.

For example, if beginning_date Year and completion_date Year are equal, I need to output beginning_date day/month - completion day/month/year.

The display and return format these fields use is: l, F j, Y

I've searched everywhere! And tried to implement this: Create display date range from two dates in ACF in Wordpress

I also attempted to convert the fields to datetime objects but comparing these as dates seems to be the wrong way! I do not know where else I'm failing at... I'm beginning again as nothing has worked so far, so I have no code to publish here because I ended up with a Frankenstein code snippet which I deleted and then came here for desperate help!

1 Answers1

1

in your functions.php

 function date_compare($id){
    
    $start_date = get_field('beginning_date', $id);
    $end_date = get_field('completion_date', $id);
    
    $start_year = wp_date("Y", strtotime( $start_date ) );
    $end_year = wp_date("Y", strtotime( $start_date ) );
    
    $start_month = wp_date("n", strtotime( $start_date ) );
    $end_month = wp_date("n", strtotime( $start_date ) );
    
    $date_output = '';
    
    $start_date_format = "l, F j, Y";
    
    if( $start_date && $end_date ){
        
        if( $start_year == $end_year && $start_month == $end_month ){
            $start_date_format = "l j";
        }
        elseif( $start_year == $end_year){
            $start_date_format = "l, F j";
        }

    }
    
    
    if( $start_date ){
        $date_output .= wp_date($start_date_format, strtotime( $start_date ) );
    }
    
    if( $end_date ){
        $date_output .= ' - ' . wp_date("l, F j, Y", strtotime( $end_date ) );
    }
    
    return $date_output;
}

and in your theme:

echo date_compare($post->ID);
Moishy
  • 3,560
  • 3
  • 23
  • 42
  • Thank you so much! I've just tested it and comparing dates' year seems to work but it outputs the start_date only. I'm testing in on a post that has the same year for star and end dates. I checked by editing $end_year = wp_date("Y", strtotime( $start_date ) ); to $end_year = wp_date("Y", strtotime( $end_date ) ); but when I did the ouput was the complete start date... Would you please help me? Thank you so much!!! I've spent so much time on this and now reading the function you posted I begin to grasp what's involved... – Mariana Scaravilli Nov 30 '22 at 10:39
  • Please ignore my previous comment! Works perfectly!!! Just needed to edit as what I wrote above. It wasn't working properly because I had the wrong format on the ACF fields. Now on to add month compare and so on. Crossing my fingers I get it right. Thank you so much! – Mariana Scaravilli Nov 30 '22 at 12:05
  • A new question: how can I include this in a query loop of custom posts? Currently, it only ouputs the date separator ' - ' – Mariana Scaravilli Nov 30 '22 at 12:21
  • You want to compare months as well in what way? – Moishy Nov 30 '22 at 12:55
  • In the loop you have to pass the post id through. If it’s missing the custom field it will only show the separator. I’ll update my code soon – Moishy Nov 30 '22 at 12:56
  • All posts with which I'm testing this have both custom fields... Could not figure out how to pass the post id thought... I very much appreciate your help! – Mariana Scaravilli Nov 30 '22 at 15:27
  • whats the code for you loop? – Moishy Nov 30 '22 at 15:38
  • can you be more specific for exactly what you want with this function? you want to compare the year AND the month? – Moishy Nov 30 '22 at 15:51
  • this is what I need to compare - if no end_date -> display start_date only (l, F j, Y) - if same year, but different month -> l, F j - l, F j, Y - if same year and same month -> l j - l j, F Y - - - - I use the queryloop block, where I've added the code via shortcode (using the code snippets plugin) - does not work within this queryloop block but it does in the single post. – Mariana Scaravilli Nov 30 '22 at 16:25
  • I updated my answer. in regards to the shortcode do the same function and pass the post id through – Moishy Nov 30 '22 at 16:39
  • 1
    Great & thank you!!! I'm testing it all later today. – Mariana Scaravilli Nov 30 '22 at 17:30
  • I don't seem to figure out how to "pass the post id through"... Could you guide me? – Mariana Scaravilli Nov 30 '22 at 18:20
  • how are you looping through your posts? the code in the shortcode – Moishy Nov 30 '22 at 18:23
  • I created a html content shortcode with "code snippets" using this: ID); ?> Code snippets creates a shortcode [code_snippet id=27 php=true] which I use as a shortcode block inside the queryloop block. This is also how I include it in the single post templates (where it works). – Mariana Scaravilli Nov 30 '22 at 19:31
  • Oh I wasnt sure thats what you were using. you can use `acf_blocks` see here - [ACF Blocks](https://www.advancedcustomfields.com/blog/build-business-listing-acf-blocks-query-loop-block-theme) – Moishy Nov 30 '22 at 23:08