Support

Account

Home Forums General Issues Display day in loop with Datepicker

Solved

Display day in loop with Datepicker

  • Hello,

    I use this code to display a carousel of events sorted by month / year on the basis of an ACF “DatePicker” field, it works very well.

    I would just like to solve 2 problems:
    1. Show my French months in no English
    2. View the recorded day for the event

    Below my code.

    Thanks for your help

    <?php
    $group_posts = array();
    
    if( $posts ) {
    
    global $post; 
    
        foreach( $posts as $post ) {
            $date = get_field('date_de_levenement', $post->ID, false);
            $date = new DateTime($date);
            $year = $date->format('Y');
            $month = $date->format('F');
            $group_posts[$year][$month][] = array($post, $date);
        } 
    } ?>
    
                                                    
    <?php
    foreach ($group_posts as $yearKey => $years) {
        foreach ($years as $monthKey => $months) { ?>
            
        <?php echo '<h4 class="blanc">' . $monthKey . ' ' . $yearKey . '</h4>';
            foreach ($months as $postKey => $posts) { 
                setup_postdata($post); ?>
    
                                                            <a href='<?php echo get_the_permalink($post->ID) ?>'>
                                                                    
                                                                <?php ///// HERE DISPLAY DAY OF THE EVENT ////// ?>
                                                                
                                                                    <span class="blanc"><?php echo get_the_title(); ?></span>
                                                            </a>
    
            <?php
            } ?>
            
    
    <?php    }
    
    } wp_reset_postdata(); 
    ?>                                            
    
  • Hi Eric,

    Your first issue : write month in French it’s not an ACF issue but a PHP one because you use ‘DateTime()‘ an ‘format‘ and this only works in English. If you want to get the month in French you can :

    • Create an array to traduce the month yourself
    • Convert your ‘DateTime()‘ into ‘Timestamp‘ to use ‘strftime‘ and a ‘setlocale()

    For the first solution, the exemple :

    Traduce_Month = array (
         "January" => 'Janvier', 
         "February" => 'Fevrier', 
          ...
    )
    echo '<h4 class="blanc">' .Traduce_Month[$monthKey]. ' ' . $yearKey . '</h4>';

    For the second one you can find all the documentation needed in PHP codex by searching ‘strftime‘ and ‘DateTime::createFromFormat‘ (to convert you date).

    For your second issue (still a PHP issue), to write the event date you just have to get the $date value you saved in your array ‘$group_posts[$year][$month][]‘ and echo it by using ‘$date->format‘ as you already done.

    Hope it’s help

  • It’s perfect.
    Thank you very much for your help!

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

The topic ‘Display day in loop with Datepicker’ is closed to new replies.