Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am building a "podcast" post type in a wordpress build using custom-post-types, meta-boxes & taxonomies. There may be a simpler way to do this.. I'm still learning and this is my first go at it. Either way, I'm almost finished, but I want to add some functionality that will help my client (make it easier to use).

One of the custom meta boxes I have is "date" - the reason for this is that the upload date for the podcast (ie the_date) is often different from the day the podcast was recorded (meta-box _date).

I know a little bit about the strtotime function in php - and I would LOVE to use this to do some processing on the data input to the "date" field on new podcasts. Right now it is just text, so if the user inputs "Jan 23, 2012" or "January 23, 2012" or "1-23-12" etc - I am stuck with that text for display purposes. However, using the strtotime function, I'm thinking I could convert the user input to a date-stamp that I could display around the site however I like (m,d,y).

I'm just not sure where to start exactly - I'm VERY new at php. I'll include the relevant code below. Any help you could provide would be very much appreciated!

Thanks!

Tim

// Podcast Meta Box

function podcast_info() {
global $post;

// Noncename needed to verify where the data originated
echo '<input id="podcastmeta_noncename" name="podcastmeta_noncename" type="hidden" value="' .     wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

// Get the data if its already been entered
$week = get_post_meta($post->ID, '_week', true);
$date = get_post_meta($post->ID, '_date', true);
$description = get_post_meta($post->ID, '_description', true);

// Echo out the field
echo '<strong>Week</strong><br /><em>What week of the series is this podcast? (enter the &lsquo;Week X&rsquo; OR &lsquo;Guest Speaker&rsquo;)</em>';
echo '<input class="widefat" name="_week" type="text" value="' . $week  . '" />';
echo '<strong>Date</strong><br /><em>Enter the Date the podcast was recorded (Format as: &lsquo;mm/dd/yy&rsquo;)</em>';
echo '<input class="widefat" name="_date" type="text" value="' . $date  . '" />';
echo '<strong>Description</strong><br /><em>Enter the text you\'d like to display as a description on the media archives list.</em>';
echo '<input class="widefat" name="_description" type="text" value="' . $description  . '" />';

}

// Save the Podcast Meta box Data

function save_podcast_meta($post_id, $post) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['podcastmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}

// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
    return $post->ID;

// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$podcast_meta['_week'] = $_POST['_week']; 
$podcast_meta['_date'] = $_POST['_date'];
$podcast_meta['_thumbnail'] = $_POST['_thumbnail'];
$podcast_meta['_seriesimg'] = $_POST['_seriesimg'];
$podcast_meta['_description'] = $_POST['_description'];

// Convert _date to actual date stamp

foreach ($podcast_meta as $key => $value) {
    if(get_post_meta($post->ID, '_date', true))
        strtotime('_date');
}

// Add values of $podcast_meta as custom fields

foreach ($podcast_meta as $key => $value) { // Cycle through the $podcast_meta array!
    if( $post->post_type == 'revision' ) return; // Don't store custom data twice
    $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
        update_post_meta($post->ID, $key, $value);
    } else { // If the custom field doesn't have a value
        add_post_meta($post->ID, $key, $value);
    }
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}

}
add_action('save_post', 'save_podcast_meta', 1, 2); // save the custom fields

EDIT - Working Now - See Code Below

<!-- Convert Date to Date Stamp -->
<?php $podcast_date = get_post_meta($post->ID, '_date', true);?>
<?php $fixed_date = strtotime($podcast_date); ?>

<!-- Display Date -->
<?php echo date('m/d/y' , $fixed_date) ; ?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You should save the date in a format that is unambiguous so you will get predictable results. The particular format you prefer can't be parsed by strtotime because it's not clear which is the month, day, and year. If you convert the date when the meta data is saved, the contents of the field will change to that format and upon second save will become unparseable. It's better to save it in a clear format and convert it when it's output to your template.

You can simplify things for your client and be sure that the format will always be parseable by adding a date picker to the meta field that enforces a particular format. This script makes it very easy. Then, in your template:

$your_meta_field_contents = 'January 23 2012';
$time = strtotime( $your_meta_field_contents );
echo date( 'm d y', $time );
share|improve this answer
    
This was extremely helpful - thanks! One issue: when I echo the date this way, I always get "01 01 70". I'm assuming something isn't happening with the Unix conversion there... –  timshutes Jan 21 '12 at 20:26
    
Never mind - I got it. I had a few issues. 1.) I put the strtotime code in before the loop... 2.) I needed to add some code to create a variable from the "_date" metadata. Once I fixed those things it began working. This is what I'm using right now.. Does this look ok, or would you do this a different way? (code added to original post) –  timshutes Jan 21 '12 at 20:46
    
@timshutes - You'll get that result if the date is in a format that strtotime can't parse. What is the date that you're inputting? echo the contents of the field before trying to convert it to verify that it's what you expect. –  Milo Jan 21 '12 at 20:49
    
Got it figured out - I had some syntax issues with the strtotime variable - I was trying to combine creating a variable out of the metadata and the strtotime code in one call. Once I separated them into two - it worked beautifully. I'll also take your suggestion and add a date-picker. I'm not sure if I'll use the one you suggested (seems overly heavy for my purposes) but either way, you've been a huge help - thanks! –  timshutes Jan 21 '12 at 20:51
    
@timshutes - if you want to DIY, WordPress includes jQuery UI and jQuery UI Datepicker, check out wp_enqueue_script for info on including it in your code. –  Milo Jan 21 '12 at 22:30
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.