Support

Account

Home Forums Add-ons Repeater Field repeater dates arranged by months title

Solving

repeater dates arranged by months title

  • Hi, I have repeater with many dates on a page.
    i would like to display the dates separated by MONTH title. – each month with its dates. and hide past dates and their month title from the page.
    sample:

    March
    23/3/2016
    27/3/2016

    April
    12/4/2016
    17/4/2016

    etc

    couldn’t find an answer…

  • Hi @felix007

    You can always sort the returned repeater data. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/.

    After that, you can use PHP datetime to check if the dates are after the current date or not.

    I hope this helps.

  • Thank you James
    I have done the repeater sort. it sorts by date. and also hide the past dates…
    but i need to separate the dates into months and hide past months –
    this is the problem i couldn’t figure.

    the code i used so far.

    <?php
    // get repeater field data
    $repeater = get_field('vacation_repeater1');
    // vars
    $order = array();
    
    if( $repeater ):
    // populate order
    foreach( $repeater as $i => $row ) {
    	$order[ $i ] = $row['start_date'];
    }
    array_multisort( $order, SORT_ASC, $repeater );
    ?>
    	<?php foreach( $repeater as $i => $row ): ?>
    <?php
    	$end_date = get_sub_field('end_date');
    			$today = date('Ymd');
    	$get_startdate = $row['start_date'];
    	$start_date = (strtotime($get_startdate));
    //use $get_startdate to order, and $start_date_pretty to output your date
    	$start_date_pretty = date_i18n( 'l, F j, Y', $start_date );
     ?>
    
    <?php if($today <= $get_startdate ) { ?>
    
    <ul>
    <li class="flight_date_time">
    	<?php echo $start_date_pretty; ?>
    </li>
    <li class="flight_date_time">
    	<?php echo $row['end_date']; ?>
    </li>
    </ul>
    <div class="clear"></div>
    
    <?php } ?>
    
    	<?php endforeach; ?>
    <?php endif; ?>
  • Hi @felix007

    If you want to sort the dates based on month and hide the past months, you can use this code:

    $repeater = get_field('repeater_field_name');
    
    $order = array();
    
    // populate order
    foreach( $repeater as $i => $row ) {
    	
    	$order[ $i ] = (int) substr($row['date_field_name'],3 ,2);
        
    }
    
    array_multisort( $order, SORT_DESC, $repeater );
    
    foreach( $repeater as $row ) {
    	
        if((int) substr($row['date_field_name'],3 ,2) >= (int) date('m')){
            echo $row['tr1_date1'];
        }
        
    }

    I hope this helps.

  • Thank you James for your replay.
    sorry but i need a different kind of sort.
    separate dates into months – so each month show its dates + month title.
    sample:

    March
    23/3/2016
    27/3/2016

    April
    12/4/2016
    17/4/2016

    also can you explain – echo $row['tr1_date1']; do i need to change it? cause i don’t get any results.

    Thank You.

  • Hi @felix007

    tr1_date1 is my date field name. I’m sorry I forgot to change it. Here is another way to achieve what you want:

    $repeater = get_field('repeater_field_name');
    $order = array();
    $now = strtotime("now");
    $sortedDate = array();
    
    foreach( $repeater as $i => $row ) {
        $order[ $i ] = $row['date_field_name'];
    }
    array_multisort( $order, SORT_ASC, $repeater );
    
    foreach($repeater as $row){
        $theDate = strtotime(str_replace('/', '-', $row['date_field_name']));
        if($theDate > $now){
            $theMonth = substr($row['date_field_name'],3 ,2);
            if (!isset($sortedDate[$theMonth])){
                $sortedDate[$theMonth] = array();
            }
            array_push($sortedDate[$theMonth], $row);
        }
    }
    
    foreach($sortedDate as $theMonth => $theDates){
        echo $theMonth;
        foreach($theDates as $theDate){
            echo $theDate['date_field_name'];
        }
    }

    If you don’t understand it, please hire a developer to help you out with it.

    I hope this helps.

  • Thank you James!
    It works as i needed!
    This will help a lot of ACF users.

    the only thing i changed is 3,2 into 4,2 at line $theMonth = substr($row['date_field_name'],4 ,2); cause my dates appear as 20160412.

    to finalize the code a question, how to echo the dates and title months to show as “pretty”?
    i used before $start_date_pretty = date_i18n( 'l, F j, Y', $start_date ); but i don’t see how i can use that in this working code.

    Thank you again

  • OK. figured it out.
    managed to change the dates to “pretty” and add the rest of the fields that i needed.
    the months name titles working… but i made them in a stupid way.
    maybe somebody can add a nice fix to it.
    hope it will help somebody as it helped me.

    
    <?php
    
    $repeater = get_field('vacation_repeater1');
    $order = array();
    $now = strtotime("now");
    $sortedDate = array();
    
    foreach( $repeater as $i => $row ) {
        $order[ $i ] = $row['start_date'];
    }
    
    array_multisort( $order, SORT_ASC, $repeater );
    
    foreach($repeater as $row){
    
        $theDate = strtotime(str_replace('/', '-', $row['start_date']));
    
        if($theDate > $now){
            $theMonth = substr($row['start_date'],4 ,2);
            if (!isset($sortedDate[$theMonth])){
                $sortedDate[$theMonth] = array();
            }
            array_push($sortedDate[$theMonth], $row);
        }
    }
    
    foreach($sortedDate as $theMonth => $theDates){
    	 //echo $theMonth . '<br/>';
    if($theMonth == 01) {
    	echo 'Jan <br/>';
    } elseif($theMonth == 02) {
    	echo 'February <br/>';
    } elseif($theMonth == 03) {
    	echo 'March <br/>';
    } elseif($theMonth == 04) {
    	echo 'April <br/>';
    } elseif($theMonth == 05) {
    	echo 'May <br/>';
    } elseif($theMonth == 06) {
    	echo 'June <br/>';
    } elseif($theMonth == 07) {
    	echo 'July <br/>';
    } elseif($theMonth == 08) {
    	echo 'August <br/>';
    } elseif($theMonth == 09) {
    	echo 'September <br/>';
    } elseif($theMonth == 10) {
    	echo 'October <br/>';
    } elseif($theMonth == 11) {
    	echo 'November <br/>';
    } elseif($theMonth == 12) {
    	echo 'December <br/>';
    }
    
    foreach($theDates as $theDate){
    	//echo $theDate['start_date']. '<br/>';
    	$date = $theDate['start_date'];
    	$date_tour = date_i18n("l, F j, Y", strtotime($date));
    	echo $date_tour. '<br/>';
    
    	echo $theDate['flight_to_time']. '<br/>';
    	echo $theDate['flight_back_time']. '<br/>';
    	echo $theDate['price_standart']. '<br/>';
    	echo $theDate['price_standart_plus']. '<br/>';
    	echo $theDate['air_company']. '<br/>';
    	echo '<br/><br/>';
    }//$theDate
    
    }//$theMonth
    
     ?>
    
  • Hi @felix007

    That’s good enough I think. But if you don’t want it to looks ugly, you can use this code:

    $monthName = date("F", mktime(0, 0, 0, $theMonth, 10));
    echo $monthName;

    But I’m not sure if it uses extra resource or not.

    Hope this helps!

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

You must be logged in to reply to this topic.