Format date to iso.8601

I’m working with the wordpress XMLRPC to post some future posts to my blog, but I’m running into some issues with date formatting… basically got myself all discombobulated 🙂

So I’ve set up the “future” post date. It’s just fine.

Read More
$thetime = date("Y-m-d H:i:s", strtotime("+ $number days", strtotime(date("Y-m-d H:i:s"))));

($thetime echos out the date i’m shooting for – so all is well here)

But the wp client wants the date in ISO.8601 format. So I changed it this way:

$content['date_created'] = date( 'c', strtotime($thetime) );

But I’m getting a response from the xml-rpc client that it’s malformed.

So how would you go about changing $thetime to iso.8601 format since I thought that’s what ‘c’ does? Am I doing something wrong?

Related posts

Leave a Reply

2 comments

  1. First, verify the outputted string and make sure that the output is what you expect. There may be a completely unrelated bug (for example a forgotten debugging echo) in there.

    Some implementations may require the date to be in UTC time. Simply use gmdate instead of date, and add a Z at the end:

    $content['date_created'] = gmdate('Y-m-d\TG:i:s\Z', strtotime($thetime)) . 'Z';
    
  2. According to XMLRPC specifications, datetime is defined in this tag <dateTime.iso8601>
    and has this format 19980717T14:08:55. So the complete tag looks like this
    <dateTime.iso8601>20090322T23:43:03</dateTime.iso8601>

    This is the common mistake when using php xmlrpc_encode_request() function, which does not automatically convert date. Rather use xmlrpc_set_type() function.

    <?php
    
    $params = date("YmdTH:i:s", time());
    xmlrpc_set_type($params, 'datetime');
    echo xmlrpc_encode($params);
    
    ?>
    

    The above example will output something similar to:

    <?xml version="1.0" encoding="utf-8"?>
    <params>
    <param>
     <value>
      <dateTime.iso8601>20090322T23:43:03</dateTime.iso8601>
     </value>
    </param>
    </params>