1

I'm trying to modify the format the date is displayed on my website, I followed this tutorial here: https://www.advancedcustomfields.com/resources/date-picker/

But, the date output is always the current date, not the date specified on the date picker.

Can you see what is wrong with this? thanks.

 <?php
    $loop = new WP_Query( array( 'post_type' => 'event', 'paged' => $paged ) );
    if ( $loop->have_posts() ) : ?>

        <ul>            
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                    <?php
                    // get raw date
                    $date = get_field('event_date');
                    // make date object
                    $date = new DateTime($date);
                    ?>

                    <li>
            <a class="event__title" href="<?php the_permalink() ?>"><?php echo get_the_title(); ?></a>
                            <span class="event__location"><?php the_field('event_location'); ?></span>
                            <span class="event__date"><?php echo $date->format('j M Y'); ?></span>
                            <a href="<?php the_permalink ?>">
                                Read more >
                            </a>
                    </li>

        <?php endwhile; ?>
            </ul>

                <?php
    endif;
    wp_reset_postdata();
?>
bazzlebrush
  • 428
  • 2
  • 15
  • Your raw date isn't matched exactly to the tutorial: `$date = get_field('date', false, false);` – Aibrean Jun 27 '16 at 18:12

1 Answers1

0

Your code seems fine. The most probable cause of your error is the return format of your event_date field.

If I'm right, enabling PHP errors should display an error like this:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string

A possible solution (maybe the easiest one) is to change the event_date field return format to something DateTime::__construct() is able to parse. For example:

Y-m-d

See:

ACF Date Picker return format settings

Community
  • 1
  • 1
Jordi Nebot
  • 3,355
  • 3
  • 27
  • 56