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.

For some reason, I'm having difficulty converting a date value stored in a meta field to a unix timestamp (and back again). If I save the date as a string it all works fine, but if I convert it to a timestamp using strtotime it won't save the value.

My working code looks like this:

<?php

add_action( 'add_meta_boxes', 'rs_add_date' );
function rs_add_date() {
    add_meta_box( 'rs_add_date', 'Date', 'rs_add_date_create_meta_box', 'concerts', 'normal', 'high' );
}
function rs_add_date_create_meta_box( $post ) {
    $date = get_post_meta($post->ID, 'rs_date', true);
    echo 'Choose a date for the event';
    wp_nonce_field( plugin_basename(__FILE__), 'rs_date_nonce');
?>
    <p>Date (dd/mm/yyyy) <input type="text" name="rs-date" id="rs-date" value="<?php echo $date; ?>"></p>
<?php
}


// Save the new meta

add_action('save_post', 'rs_date_save_meta');
function rs_date_save_meta( $post_id ) {

    if(!wp_verify_nonce( $_POST['rs_date_nonce'], plugin_basename(__FILE__) ) )
        return;
    if(!current_user_can('edit_posts') )
        return;
    $date = $_POST['rs-date'];
    update_post_meta($post_id, 'rs_date', $date);
}
?>

But if I change it to the following it won't work:

<?php
add_action( 'add_meta_boxes', 'rs_add_date' );
function rs_add_date() {
    add_meta_box( 'rs_add_date', 'Date', 'rs_add_date_create_meta_box', 'concerts', 'normal', 'high' );
}
function rs_add_date_create_meta_box( $post ) {
    $date = get_post_meta($post->ID, 'rs_date', true);
    $date = time("d/m/Y", $date);
    echo 'Choose a date for the event';
    wp_nonce_field( plugin_basename(__FILE__), 'rs_date_nonce');
?>
    <p>Date (dd/mm/yyyy) <input type="text" name="rs-date" id="rs-date" value="<?php echo $date; ?>"></p>
<?php
}


// Save the new meta

add_action('save_post', 'rs_date_save_meta');
function rs_date_save_meta( $post_id ) {

    if(!wp_verify_nonce( $_POST['rs_date_nonce'], plugin_basename(__FILE__) ) )
        return;
    if(!current_user_can('edit_posts') )
        return;
    $date = $_POST['rs-date'];
    $date = strtotime($date);
    update_post_meta($post_id, 'rs_date', $date);
}
?>

Scratching my head here! Cheers

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Looking at http://www.php.net/manual/en/datetime.formats.date.php I dont think strtotime will convert a DD/MM/YYYY to time correctly.

However it can do MM/DD/YYYY or YYYY/MM/DD.

Try using the date format of YYYY/MM/DD

Or if thats not to your liking then you can use the same date format but you will have to, on save, split up the date and convert it to unix datestamp a diff way. You could use:

$date = "dd/mm/yyyy";
$date = explode("/", $date);
$date = mktime(0, 0, 0, (int)$date[1], (int)$date[0], (int)$date[2]);
share|improve this answer
    
cheers for that - you're absolutely right. RTFM I guess! –  Richard Sweeney Apr 12 '11 at 14:20
    
@Richard - No problem, I ran into the same problem many a time. I do hate that PHP chose to give US format presidency over UK date format but then again US has a larger population :( But you can understand why strtotime() cannot check a UK date format. Take the date 01/02/2011. UK and US see that as different dates but there is no way to tell if you mean UK or US date format. What would be nice is a function where you can feed in the inputted date format: strtotimeformat("01/01/2011", "d/m/Y", time()); –  Brady Apr 12 '11 at 14:31
    
Agreed, it seems like a real oversight that the UK (and Irish!!) date format wasn't taken into consideration when creating the strtotime function. Thanks again for the help! –  Richard Sweeney Apr 13 '11 at 8:25
add 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.