Support » Fixing WordPress » Custom Field as Date won’t explode()

  • Resolved mauricenaef


    Hi there

    having some issues with a custom field I have formatted as date (yyyy-mm-dd).
    Now I would like to explode the value with explode("-", $value) but all I get are no values or array in the output.

    Here is the code I use, maybe someone can help?

    $customdate = get_post_meta($post->ID, "date_value", true);
    			list($day, $month, $year) = explode("-", $customdate);

    After that I would like to echo with $day, etc. in the respective fields.

    Thanks in advance for any suggestions on what I might be missing…

    BR Marcel

Viewing 13 replies - 1 through 13 (of 13 total)
  • What is output if you do..

    print_r( explode("-", $customdate) );



    did you echo $customdate, to make sure that it contains what you expect?



    @t31os_Member, I get Array ( [0] => )? Only when I echo get_post_meta($post->ID, "date_value", true); I receive the value 2010-01-22 which is the date in the custom field? That’s why Im really scratching my head!



    @alchymath when I echo $customdate I don’t get any value at all…

    maybe it would help to see the whole query…

    <ul id="eventslist">
    <?php //The Query
    		$customdate = get_post_meta($post->ID, "date_value", true);
    		list($day, $month, $year) = explode("-", $customdate);
    //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="eventdate">
    <div class="month m-<?php the_time('m') ?>"><?php the_time('M') ?></div>
    <div class="day d-<?php the_time('d') ?>"><?php the_time('d') ?></div>
    <div class="year y-<?php the_time('Y') ?>"><?php the_time('Y') ?></div>
    <h3><?php the_title(); ?></h3>
    <p><?php echo $customdate ?></p>
    <?php endwhile; ?>
    <?php else: ?>
    <h2>Keine Events</h2>
    <?php endif; wp_reset_query(); ?>

    Are you saying the following both produce differing output..

    echo get_post_meta($post->ID, "date_value", true);


    $customdate = get_post_meta($post->ID, "date_value", true);
    echo $customdate;

    ..they should essentially produce the same..



    If I input exactly what you suggest I get the same values, but if I just try and output as mentioned above It doesn’t output at all with echo $customdate;

    Could It have something to do with the loop? Sorry I’m really a php newbie..



    If I attach all in one string like:

    <?php $customdate = get_post_meta($post->ID, "date_value", true); list($day, $month, $year) = explode("-", $customdate); echo $day;?>

    then it works, but I thought it sould be possible with normal php variables to make the code more legiable…. or am I mistaken?

    Ok, i see where you’re heading, but why the list, what’s the purpose it’s suppose to serve other then being a bit more legible.

    A fix for the problem might be to explode the string before the list… ( it may not ) …

    Also note, the variables in the list go in the order of the items in the array (the explode), so it should actually be.. list( $year , $month , $day )



    Hi t31os_

    got that with the order… but the list’s purpose is so I can output the values on different places within the loop, as in the example above I’m pulling out the dates to replace the values for all the the_time() values as I need a special date (custom field) and not the regualr wordpress post date if you understand….

    I’m still very confused why it’s behaving so strange…

    anyway will further pick my head… thank’s thou for the help so far!
    BR Marcel

    As long as this code is inside the loop it should be fine.

    	$customdate = get_post_meta( $post->ID , 'date_value' , true );
    	list( $year , $month , $day ) = explode( '-' , $customdate );

    ..else no $post->ID will exist.. (which you’re referencing in get_post_meta)

    Works on my test install (in the loop).



    Seems as if that was the problem!! Just figured it out to!
    Thanks so much for the hint.. sometimes its the obvious which goes under!

    Thanks again…

    You’re welcome… 😉

    Hi I have this exact issue. Can you (someone) please post the final working code?

    I’m ultimately trying to show all posts with “event_date” that equals today. Then, on a separate page, all posts with “event_date” that falls within this week, for example.

    I have a feeling I need to explode the custom field somehow out side of the loop in order to do the comparison and then the query.

    I am also very much a noob at this, so please excuse me if I’m not getting how this works.

    Thanks in advance!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Custom Field as Date won’t explode()’ is closed to new replies.