尝试通过json导入后期数据时解决反转strtotime()问题

时间:2017-03-23 13:52:39

标签: php arrays json

我正在导入以json格式提取的博客内容。我无法获取导入日期,因为它目前采用strtotime()格式。我需要在导入时将其反转以便正确导入日期。任何建议或帮助将不胜感激。

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数据:

 "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"
  }
]

1 个答案:

答案 0 :(得分: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);
}