Support

Account

Home Forums Backend Issues (wp-admin) ACF Date Picker — Format

Solved

ACF Date Picker — Format

  • Hello there,

    I meet problems with the DatePicker because I want to use a custom format to some place of my project.

    <?php
    $event_date = get_field(‘event_date’); // field (group)
    $date = $event_date[‘event_start-date’]; // subfield (date picker)

    echo $date_day = date(“j”, strtotime($date));
    echo $date_month = date(“M”, strtotime($date));
    echo $date_year = date(“Y”, strtotime($date));
    ?>

    I need to show Day / Month / Day separately because of a custom design.

    $date_month : return the correct one (most of the time)
    $date_day : return the day with an offset +1 instead of the correct day
    $date_year : return the correct year (most of the time)

    I say “most of the time”, because sometimes it return a default date “1st January 1970”.

    I tried all solutions coming from this post (https://support.advancedcustomfields.com/forums/topic/date-output/) but nothing worked for me. I search for that for long time, please help would be strongly appreciated.

    The only thing that always work correctly is just “echo $date;” but not what I’m looking for.

    Thanks in advance for your help!

  • I’m having the same issue. Depending on the solution I use on the forums, when trying to convert the date to a custom format I either see: (1) Jan 1 1970 as you’re seeing, or (2) the current date, which I have confirmed as it has changed as this problem is taking a while to research and resolve.

    Has anyone else found any solutions yet?

  • I finally found a solution. When creating the custom fields, make sure you choose the Ymd return format. With other return formats, this does not work.

    Then in the code, get the field (or subfield, in my case): $date = get_sub_field('date');.

    Convert using PHP’s DateTime: $eventDate = new DateTime($date);

    And display using your required format, for example: $eventDate->format('j');

    Hope this helps!

  • Hey mckenna,

    Yes, I’ve had finally found the same solution. It seems that this is the only way to make it works. Thanks for your reply !

    My piece of code :

    <?php
    // get the publishing date
    if ( $date = get_field('publishing-date') ) {
      $dateTime = DateTime::createFromFormat("Ymd", $date);
    
      if ( is_object($dateTime) ) { 
        setlocale(LC_TIME, 'fr_FR' );
        $date = $dateTime->format('j F Y'); 
        $year = strftime('%Y', strtotime($date));
        $month = strftime('%m', strtotime($date));
        $day = strftime('%d', strtotime($date));
        $date = strftime('%e %B %Y', strtotime($date));
      } 
    } ?>
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘ACF Date Picker — Format’ is closed to new replies.