0

I need to format the date output in the following WordPress PHP loop.

The current output looks like this: 2019-07-10 12:00:00

I would like it to look like this: July, 10th, 2019 (time not needed)

<?php       
    $event_query = new WP_Query(
        array(
        'post_type' => 'tribe_events',               
        'order' => 'asc',
        'posts_per_page' => 2,                                        
        ) //end array 
    ); ?>
    <?php while ($event_query->have_posts()) : $event_query->the_post(); ?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <?php
        $meta_print_value=get_post_meta(get_the_ID(),'_EventStartDate',true);
        echo($meta_print_value);
        ?>
        <?php the_excerpt(); ?>
        <?php the_permalink(); ?>">View Event</a>
    <?php endwhile; wp_reset_query(); ?>

I've tried several techniques, but can't get the PHP just right.

josefresco
  • 143
  • 7
  • 5
    Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Shardj Jul 10 '19 at 15:41

2 Answers2

1

You can format a date with PHP's built in date function, see the documentation for all possibilities.

Here is what you'd like to achieve:

date('F, jS, Y', strtotime($wp_date));

Fiddle:

https://3v4l.org/WOCPd

Andrew
  • 827
  • 2
  • 6
  • 14
  • Thank you for your comment. I guess I need help putting it altogether. Do I replace this line? $meta_print_value=get_post_meta(get_the_ID(),'_EventStartDate',true); – josefresco Jul 11 '19 at 14:04
  • I'm not 100% on how your code works, but I believe the line you copied here declares the date you tried to format as a variable. So far so good. If that's the case, you need to change the next line: instead of echo($meta_print_value); - try: echo date('F, jS, Y', strtotime($meta_print_value)); – Andrew Jul 11 '19 at 15:19
  • Thank you Andrew! That worked like a charm. No idea why I couldn't figure that out on my own. – josefresco Jul 11 '19 at 17:48
0

After $meta_print_value use below shared code:

$meta_val=date('F,S,Y',strtotime($meta_print_value));

Also replace echo($meta_print_value); into echo $meta_val;

Dharman
  • 30,962
  • 25
  • 85
  • 135