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.

In my articles I allow editors to enter a date in a human readable dd.mm.yyyy format into a custom field. But I also need this date as unix timestamp in the DB. So I wrote a function which takes the human readable date from the custom field "Event-Date", converts it into a unix timestamp and then saves it into a custom field "Event-Timestamp". I trigger this function with 'add_action' 'save_post'.

The script works fine, but the problem is that if I later change the dd.mm.yyyy date and then save the article, my script kicks in BEFORE the new values are registered. This results in the old date still being used for the timestamp calculation.

For example:

In the article the dd.mm.yyyy date is 01.01.2011. Now I change it to 02.02.2012 and save the changes, but the unix timestamp will still be for 01.01.2011! If I then change 02.02.2012 to 03.03.2013 and save the changes, the unix timestamp now becomes 02.02.2012! And so on...

Here is my function:

function post_saver($postID) {
    $limitPostTypes = array('post');

    if (!in_array($_POST['post_type'], $limitPostTypes)) return;

    $custom_field   = 'Event-Date';
    $newFieldName   = 'Event-Timestamp';

    foreach ($_POST['meta'] as $meta)
        if ($meta['key'] == $custom_field)
            $date   = $meta['value'];

    $date           = explode('.', $date);
    $dateYear       = $date[2];
    $dateMonth      = $date[1];
    $dateDay        = $date[0];

    $unixTimeStamp  = mktime(0, 0, 0, $dateMonth, $dateDay, $dateYear);

    update_post_meta($postID, $newFieldName, $unixTimeStamp);
add_action( 'save_post', 'post_saver');

Any help or suggestions would be highly appreciated! Thanks in advance!

share|improve this question

closed as too localized by toscho Feb 19 '13 at 0:02

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

See the end of the second answer of wordpress.stackexchange.com/questions/15546/…. This might help. –  jot Dec 20 '11 at 19:35
add comment