Support » Fixing WordPress » Changing the output of date from a custom field

  • rccard

    (@rccard)


    I am working on an events page for a website I am designing with WordPress as the CMS. The date displays YYYY/MM/DD. I would like to output date like this Wednesday, March 7, 2012, not 2012/03/07.

    I need to get the date in the YYYY/MM/DD format in order for the function to know how to sort and effectively stop displaying the post once the day comes. I enter this date in a custom field when creating the post. I have set up a custom post type to handle events.

    I’m a noob when it comes to WordPress and PHP but here is the code I am using to output the events.

    <?php if (have_posts()) : ?>
                    <?php /* edit begin */ ?>
    	        <?php query_posts(
    		array (
                        'post_type' => 'event',
                        'posts_per_page' => -3,
                        'meta_key' => 'Date',
                        'meta_value' => date("Y/m/d"),
                        'meta_compare' => '>=',
                        'orderby' => 'meta_value',
                        'order' => 'ASC',
                        'paged' => get_query_var('paged')
                        )
    	            );
                        $more = 0;
    		    ?>
                        <?php /* edit end */ ?>
                        <?php while (have_posts()) : the_post(); ?>
                        <div class="blogpost">
                        <h1 class="title" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
                        <p class="post-meta"><span style="color: #168f9d">Date: </span><?php if ( function_exists('get_custom_field_value') ){ get_custom_field_value('Date', true); } ?> <?php edit_post_link('Edit this post <br />', '| ', ''); ?></p>

    Like I said, I’m new to all of this so I’m feeling a little overwhelmed. Is there a way to change the date format in the call to display it? I have searched many forums and blogs to find an answer but with NO luck. I could leave the display as YYYY/MM/DD but it doesn’t look as user friendly.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this example:

    echo date("l, F j, Y", strtotime("2012/03/07"));

    change “2012/03/07” with the date custom field value.

    Thread Starter rccard

    (@rccard)

    Thank you for the reply.

    As I said, I am a noob when it gets into some of this stuff. I placed this line in my code and now it outputs “Thursday, January 1, 1970” for every event. So, I guess I’m getting close in one way.

    My custom field is called ‘Date’, I’m not sure what I have to put in the strtotime(“????”).

    Sorry if I’m making something simple complicated.

    Moderator keesiemeijer

    (@keesiemeijer)

    This is not a native WordPress function:

    get_custom_field_value('Date', true);

    I don’t know what it returns but try this:

    $date = '';
    if ( function_exists('get_custom_field_value') ){
    $date = get_custom_field_value('Date', true);
    }
    if($date != ''){
    echo date("l, F j, Y", strtotime($date));
    }

    or try it with a WordPress function:

    $date = get_post_meta($post->ID, 'Date', true);
    
    if($date != ''){
    echo date("l, F j, Y", strtotime($date));
    }

    Are you sure ‘Date’ is the custom field key? php date() has “Thursday, January 1, 1970” as it’s default value (timestamp) if the second argument (strtotime($date)) is an empty string.
    http://php.net/manual/en/function.date.php

    Thread Starter rccard

    (@rccard)

    The wordpress function did the job! Thanks!

    Moderator keesiemeijer

    (@keesiemeijer)

    No problem. Glad you got it resolved.

    I’ve been looking at implementing something similar to this and found a solution using

    get_custom_field_value('Date', true);

    and it looks something like

    $date = '';
    if ( function_exists('get_custom_field_value') ){
    $date = strtotime(get_custom_field_value('Date', false));
    }
    if($date != ''){
    echo date("l, F j, Y", ($date));
    }

    Hope it helps
    Incidentally this is my first post.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Changing the output of date from a custom field’ is closed to new replies.