Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Split custom meta date WordPress

  • SOLVED

I have a meta box that adds a date to an event (Eg 24 August 2012). I need to split the date so that it displays as follows follows:

<div class="date">
<div class="number">24</div>
<div class="month">August</div>
<div class="year">2012</div>
</div>


<strong>Meta Box</strong>

array(
'name' => 'Start Date',
'id' => "meta_event_date",
'type' => 'date',
'format' => 'd MM, yy'
),


<strong>Current loop (shows full date)</strong>

<?php
global $post;
$tmp_post = $post;
$args = array('numberposts' => 99, 'post_type'=> 'event_post', 'category'=> '-16');
$myposts = get_posts( $args );
foreach($myposts as $post) :
setup_postdata($post);
?>
<li class="event">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="date">
<div class="number"><?php $meta_event_date = get_post_meta( get_the_ID(), 'meta_event_date', true ); ?><?php echo $meta_event_date ?></div>
<div class="month"></div>
<div class="year"></div>
</div>

<h3><?php the_title(); ?></h3>
</a>
</li>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>

Answers (1)

2012-08-22

Luis Abarca answers:

Use explode

<?php
$meta_event_date = get_post_meta( get_the_ID(), 'meta_event_date', true );

$date = explode(' ', $meta_event_date)
?>

<div class="date">
<div class="number"><?php echo $date[0] ?></div>
<div class="month"><?php echo $date[1] ?></div>
<div class="year"><?php echo $date[2] ?></div>
</div>


Luis Abarca comments:


<?php
global $post;
$tmp_post = $post;

$args = array('numberposts' => 99, 'post_type'=> 'event_post', 'category'=> '-16');

$myposts = get_posts( $args );
foreach($myposts as $post) :
setup_postdata($post);

$meta_event_date = get_post_meta( get_the_ID(), 'meta_event_date', true );

$date = explode(' ', $meta_event_date);

?>
<li class="event">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="date">
<div class="number"><?php echo $date[0] ?></div>
<div class="month"><?php echo $date[1] ?></div>
<div class="year"><?php echo $date[2] ?></div>
</div>

<h3><?php the_title(); ?></h3>
</a>
</li>

<?php endforeach; ?>
<?php $post = $tmp_post; ?>


Nick comments:

Almost, the output has a comma after the month, how to remove that?

Output


<div class="date">
<div class="number">24</div>
<div class="month">August<strong>,</strong></div>
<div class="year">2012</div>
</div>


Luis Abarca comments:

This is a quick solution



<div class="month"><?php echo str_replace(',', '', $date[1]) ?></div>


Nick comments:

Great that worked, thank you very much.

Last thing, I just realised I need to display the short version of the month eg Aug as opposed to August, any idea on how I would do that?

Thanks

<div class="date">
<div class="number">24</div>
<div class="month"><?php echo str_replace(',', '', $date[1]) ?></div>
<div class="year">2012</div>
</div>


Luis Abarca comments:

This is a best solution, you can use M instead of F, to show Aug


<?php
global $post;
$tmp_post = $post;

$args = array('numberposts' => 99, 'post_type'=> 'event_post', 'category'=> '-16');

$myposts = get_posts( $args );

foreach($myposts as $post) :
setup_postdata($post);

$meta_event_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_date', true ) );
?>

<li class="event">

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">

<div class="date">

<div class="number"><?php echo date('j', $meta_event_date) ?></div>
<div class="month"><?php echo date('F', $meta_event_date) ?></div>
<div class="year"><?php echo date('Y', $meta_event_date) ?></div>
</div>

<h3><?php the_title(); ?></h3>
</a>
</li>

<?php endforeach; ?>
<?php $post = $tmp_post; ?>


Nick comments:

Thanks, how would that work with explode and the str_replace on the comma?


Luis Abarca comments:

Dont worry, you dont need str_replace, because we are working with dates instead of strings, [[LINK href="http://codepad.org/nir628sQ"]]http://codepad.org/nir628sQ[[/LINK]]


Nick comments:

PERFECT Thank You!


Luis Abarca comments:

you are welcome!