Warning when trying to convert timestamp to readable format foreach instance while looping thorugh an array

心不动则不痛 提交于 2019-12-25 14:41:24

问题


Warning when trying to convert timestamp to readable format foreach instance while looping through an array.

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

    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[$date];

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

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

Warning: date() expects parameter 2 to be integer, string given in /Users/andrew/Desktop/newtest.php on line 6

Any help or suggestions will be greatly appreciated.


回答1:


You need to strtotime the variable

$date = date('m/d/Y H:i:s', strtotime($dateCreated))


来源:https://stackoverflow.com/questions/42955475/warning-when-trying-to-convert-timestamp-to-readable-format-foreach-instance-whi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!