Home  >  Q&A  >  body text

Change how dates are formatted in custom fields in WordPress

I created a custom field 'Event date' for my custom post type 'Event'. I'm trying to display a date on the front end and want to change the display format of the date but it doesn't seem to work. Here is the code I tried:

<div class="single-post-event-meta">
                <p><?php 

                    $event_date = get_post_meta(get_the_ID(), '_event_post_date_value_key');
                    echo $event_date[0]->format('M');
                
                ?></p>
            </div>

Thanks for the help!

P粉180844619P粉180844619431 days ago630

reply all(1)I'll reply

  • P粉187677012

    P粉1876770122023-09-08 19:00:29

    Look, I answered my own question! WoohoooO!

    <p class="single-post-event-meta-date"><?php 
       echo '日期:';
       echo (new DateTime(get_post_meta($post>ID,'_event_post_date_value_key',true)))->format('d M Y');
     ?></p>

    reply
    0
  • Cancelreply