Support

Account

Home Forums Add-ons Repeater Field Change Date format in Repeater Field

Solving

Change Date format in Repeater Field

  • Hello

    I allow myself to use the forum because I have not found an answer
    to my problem by browsing it.

    I use a repeater field called: “dates”
    Below this field are the following subfields: date_depart / date_retour

    Here is the “visual” structure:
    [Repeater “dates”]
         [Sub_Field “date_depart”] [Sub_Field “date_retour”] +
         etc …

    The output format of the date configured in the backoffice is: l d F Y
    but it must appear in 2 different formats in my website.

    I would like to display the date in another format in this form: F Y

    Precision I only need the date_depart of the first line of my “repeater”.
    These are fields that display periods of travel, and I only need the departure date (Month / Year).

    Here is my PHP code:

    if (have_rows ('dates')):
    
    # Recovery of the "Dates" field
    $ firstDates = get_field ('dates');
    # We stop on the first item of the period
    $ first_depart = $ firstDates [0] ['start_date'];
    
    echo ($ first_depart);

    I want to change $ first_depart> in the form ‘F Y’.

    I tried different methods to convert the date of entry to another format but without success …
    What is the best way to reach my goals?

    I need a little help 😉
    Thanks for your help.

  • I would do this by setting my return value to be formatted as Y-m-d and then I would format the date/time the way I want by using PHP function date() and strtotime(). The reason for setting the display to Y-m-d is because this is a format that will not be confused when using strtotime()

    
    $acf_date = get_field('my_date_field');
    
    // format date
    $date = date('l d F Y', strtotime($acf_date));
    
    // OR
    $date = date('F Y', strtotime($acf_date));
    

    http://php.net/manual/en/function.strtotime.php
    http://php.net/manual/en/function.date.php

  • Good morning, John

    Thank you very much for your answer, however it seems that my problem is more complicated than it seems…

    Because I have to change the first “date” of a line of my repeater only, it is not a single date field.

    I tried to modify my code as well to test:

    
    <?php 
    # Affichage du Mois / Année de la période (1er Element / Date de départ)
    if( have_rows('dates') ):
    
    # Recuperation du champs "Dates"
    $firstDates   = get_field('dates');
    
    # On stop sur le premier item de la période
    $first_depart = $firstDates[0]['date_depart'];
    // format date
    $date1 = date('l d F Y', strtotime($first_depart));
    // OR
    $date2 = date('F Y', strtotime($first_depart));
    
    echo($date1);
    echo($date2);
    													endif;
    ?>
    

    Here are some screenshots of my configuration of my dates fields:
    Repeater Field (Dates)
    Field 'Date_depart'

    I get the date of January 1, 1970
    I think there is a problem beyond me.

    And the result of the displayed code:
    Display on Website

    Thank you again for your help !
    Bertrand

    *************
    Edit – Capture (IMG) are not showing…

    My Repeateur Field
    https://ibb.co/kGaRZw

    Date Sub_Field
    https://ibb.co/hPOkSG

    Display on Website
    https://ibb.co/g3Dr1b

  • I found the solution …!
    I will post soon for those who are interested.

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

You must be logged in to reply to this topic.