• Hi, I got some “custom posts” set for events and I am ordering the events by the input date in a “custom field” in Year/Month/Day format.

    I’d like to convert the output of that format to “06 February 2011”, for example, in the frontend.

    I am triyng this but it doesn’t work


    <?php
    $todaysDate = date('Y/m/d');// Get today's date in the right format
    $agendaloop = new WP_Query('post_type=agenda&posts_per_page=10&meta_key=_date&meta_compare=>&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');

    $display_date = date('d F, Y', strtotime(get_post_meta($post->ID, "_date", true)));
    ?>

    Than in the “loop” I got this for the output:

    <?php echo $display_date; ?>

    I get 01 January 1970 for all the posts. I know that this is the start date for “strtotime”, but what’s wrong with the code?

Viewing 5 replies - 1 through 5 (of 5 total)
  • try this <?php the_time((get_post_meta($post->ID, "_date", true)) ?>

    C
    https://www.marketingadsandseo.com

    Thread Starter Gianfranco

    (@gian-ava)

    Where should the code go?

    In the “loop”, or in the “query”?

    I am able to pull the custom field, I just need to convert the format.
    Thanks.

    the conversion idea seems to work;
    however, the result of
    get_post_meta($post->ID, "_date", true)
    seems to be empty.

    you mention:

    Then in the “loop” I got this for the output:

    <?php echo $display_date; ?>

    does this mean that this code:
    $display_date = date('d F, Y', strtotime(get_post_meta($post->ID, "_date", true)));
    is outside the loop?

    have you tried to move this code to inside the loop?

    another possibility is to try to use get_the_ID() instead of $post->ID in the get_post_meta() code

    Thread Starter Gianfranco

    (@gian-ava)

    alchymyth, that did it.
    Thanks a lot, I appreciate.

    If you have a look at my code in the first post, you’ll notice the $display_date was in the query, not inside the loop. i moved it and it worked fine.
    Got what I needed.
    Cheers to you.
    ??

    you are welcome ??

    If you have a look at my code in the first post, you’ll notice the $display_date was in the query

    that is exactly what i did ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Converting a date format from a custom field’ is closed to new replies.