Forums

Converting a date format from a custom field (6 posts)

  1. gian-ava
    Member
    Posted 1 year ago #

    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?

  2. MAS
    Member
    Posted 1 year ago #

    try this <?php the_time((get_post_meta($post->ID, "_date", true)) ?>

    C
    http://www.marketingadsandseo.com

  3. gian-ava
    Member
    Posted 1 year ago #

    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.

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    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

  5. gian-ava
    Member
    Posted 1 year ago #

    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.
    ;-)

  6. alchymyth
    The Sweeper
    Posted 1 year ago #

    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 ;-)

Topic Closed

This topic has been closed to new replies.

About this Topic