0

I am importing blog content that I have extracted in json format. I am having trouble getting the date to import because it is currently in strtotime() format. I need to reverse this when importing in order to import the date correctly. Any suggestions or help will be appreciated.

PHP

<?php
$json_feed = "http://localhost/sample/json/sample.json";
$json = file_get_contents($json_feed);
$obj = json_decode($json, true);
$date = date('Y/m/d H:i:s', strtotime($obj['post'].dateToPublish));

foreach($obj['post'] as $article_array){
    $url = $article_array['url'];
    $title = $article_array['title'];
    $category = $article_array['category'];
    $large_summary = $article_array['wp_post_content'];
    $date = $article_array['dateToPublish'];

    $post = array(
        'post_title' => $title,
        'post_content' => $large_summary,
        'post_status' => 'publish',
        'post_type' => 'post',
        'comment_status' => 'closed',
        'date' => $date,
        'post_template' => 'content.php'
        );

    wp_insert_post ($post, $wp_error);
}
?>

JSON data:

 "post": [
  {
    "updatedByUser": "Author-Name",
    "author": "Author-Name",
    "dateToPublish": 1418869808504,
    "nid": 2413,
    "contentId": "54923c30202946517983d30d",
    "type": "post",
    "title": "This is a post title!",
    "wp_post_content": "This is post content!",
    "not_type": "post",
    "datePublished": 1418869808504,
    "dateCreated": 1263356908,
    "appLength": 0,
    "idOLD": "54923c30202946517983d30d",
    "slug": "This-is-a-slug",
    "createByUser": "Author-Name"
  },

  {
    "updatedByUser": "Author-Name",
    "author": "Author-Name",
    "dateToPublish": 1418869808508,
    "nid": 2420,
    "contentId": "54923c30202946517983d314",
    "type": "post",
    "title": "This is a title!",
    "wp_post_content": "This is post content!",
    "not_type": "post",
    "datePublished": 1418869808508,
    "dateCreated": 1265775472,
    "appLength": 0,
    "idOLD": "54923c30202946517983d314",
    "slug": "This-is-a-slug",
    "createByUser": "Author-Name"
  }
]
Andrew
  • 3
  • 2
  • Wow, I've seen this already. You still concatenate `$obj['post'].dateToPublish` where `dateToPublish` is not defined. – u_mulder Mar 23 '17 at 13:54
  • Here http://stackoverflow.com/questions/42955475/warning-when-trying-to-convert-timestamp-to-readable-format-foreach-instance-whi – u_mulder Mar 23 '17 at 13:55
  • No one can solve this task in your company? – u_mulder Mar 23 '17 at 13:55
  • datePublished is just time with milliseconds added. Take the last 3 numbers off, and you can get a proper date using the date() function. It's interesting that dateCreated and datePublished are 4 years apart, though. – aynber Mar 23 '17 at 13:57
  • @aynber maybe it's a job interview task?))) – u_mulder Mar 23 '17 at 13:58
  • @u_mulder Hmm, good point. Considering some of the data (I'm guessing the OP didn't replace it), you're probably right. – aynber Mar 23 '17 at 13:59
  • Hint: `$obj['post'].dateToPublish` is how you can get array values in javascript. It's not how it works in PHP. – aynber Mar 23 '17 at 14:01
  • Both OPs are from Kentucky, btw. – u_mulder Mar 23 '17 at 14:01
  • Thank you for the suggestions. The stackoverflow link you provided was a post from me and an intern yesterday. This is not an interview question. I have a job making (clicking and dragging) websites in wordpress and I am trying to branch out and learn new things by working on these projects to help me complete work tasks much more efficiently. – Andrew Mar 23 '17 at 14:17
  • do a `print_r($obj)` to get a better idea on how it looks like. – apokryfos Mar 23 '17 at 14:20
  • If the solution is to remove the last 3 numbers, how could this be done in bulk? I have over 3 json files that need to be imported with over 100,000 posts in the arrays. The only piece I cannot get to work is the date. – Andrew Mar 23 '17 at 14:21
  • Possible duplicate of [How to convert a 13 digit Unix Timestamp to Date and time?](http://stackoverflow.com/questions/32926749/how-to-convert-a-13-digit-unix-timestamp-to-date-and-time) – aynber Mar 23 '17 at 14:39

1 Answers1

0
<?php 
$json_feed = "http://localhost/sample/json/sample.json";
$json = file_get_contents($json_feed);
$obj = json_decode($json, true);

foreach($obj['post'] as $article_array){
    $title = $article_array['title'];
    $content = $article_array['wp_post_content'];
    $date = $article_array['dateToPublish'];

$post = array(
    'post_title' => $title,
    'post_content' => $content,
    'post_status' => 'publish',
    'post_type' => 'post',
    'comment_status' => 'closed',
    'post_date' => date('Y-m-d H:i:s', bcdiv($date, '1000')),
    'post_template' => 'content.php'
    );


wp_insert_post ($post, $wp_error);
}
Andrew
  • 3
  • 2