Php JSON已创建\u时间转换preg\u替换$string输入

Php JSON已创建\u时间转换preg\u替换$string输入,php,facebook,json,graph,preg-replace,Php,Facebook,Json,Graph,Preg Replace,这很难描述。因此,我使用Facebook图表在网站上显示我的页面Facebook/twitter(通过Facebook)的视图。一切都很好,除了我正在尝试将创建的时间转换成更好的格式。我已经测试了时间的撅嘴,以确保它的工作,就像这样 $string = 2012-02-06T19:38:35+0000 但是,当我尝试动态传递$data->created\u time;它不喜欢它得到的,所以它说日期是1970年1月1日。我知道它可能得到一些不可读的东西,但我看不出它是什么以便诊断它/ 这是所有的

这很难描述。因此,我使用Facebook图表在网站上显示我的页面Facebook/twitter(通过Facebook)的视图。一切都很好,除了我正在尝试将创建的时间转换成更好的格式。我已经测试了时间的撅嘴,以确保它的工作,就像这样

$string = 2012-02-06T19:38:35+0000
但是,当我尝试动态传递$data->created\u time;它不喜欢它得到的,所以它说日期是1970年1月1日。我知道它可能得到一些不可读的东西,但我看不出它是什么以便诊断它/

这是所有的代码

<?php 

$info = json_decode(file_get_contents('https://graph.facebook.com/210849652406/feed/?access_token=[ACCESS_TOKEN]&limit=20'));

$string =  $info->data->created_time;
$pattern = '/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\+(\d{4})/i';
$replacement = '$3-$2-$1 $4:$5:$6';
$new_time = preg_replace($pattern, $replacement, $string);


 $dTime = strtotime($new_time);
 //format the date / time into something human readable
 //if you want it formatted differently look up the php date function
 $myTime=date("M d Y h:ia",$dTime);


if ($info) {

foreach ($info->data as $obj) {
    if ($obj->application->name =='Twitter') {
    echo "<div class=\"ca-item\"><div class=\"ca-item-main-twitter\">";
    echo "<div><span class=\time\><a href=\"https://twitter.com/#!/ouhsd\">@OUHSD--", $myTime,"</a></span>";
    echo "<span class=\"content\">", $obj->message, "</span></div>" ;
    echo "<div class=\"us\"></div>";
    echo "</div></div>";
    }
}   
}
?>

提示:当你饿的时候,不要为了解决问题而推迟午餐时间。你很可能会在午饭后解决这个问题,那时你不会挨饿,也不会急于解决这个问题。答案比我想象的要简单

解决方案

<?php 

$info = json_decode(file_get_contents('https://graph.facebook.com/210849652406/feed/?access_token=305397012843053|0Lge1zfZW1kE77tnJ0gbmcGLNuo&limit=20'));

if ($info) {

foreach ($info->data as $obj) {

 $dTime = strtotime($obj->created_time);
 //format the date / time into something human readable
 //if you want it formatted differently look up the php date function
 $myTime=date("M d Y h:ia",$dTime);
        if ($obj->application->name =='Twitter') {

    echo "<div class=\"ca-item\"><div class=\"ca-item-main-twitter\">";
    echo "<div><span class=\time\><a href=\"https://twitter.com/#!/ouhsd\">@OUHSD--", $myTime,"</a></span>";
    echo "<span class=\"content\">", $obj->message, "</span></div>" ;
    echo "<div class=\"us\"></div>";
    echo "</div></div>";
    }
}   
}
?>


我试图在调用它之前修复时间,我有不必要的

如果
$string
2012-02-06T19:38:35+0000
,你不能跳过正则表达式并执行
strottime($string)
?谢谢你指出这一点,我有一些不必要的preg_替换正在进行,因为我认为时间戳对于strotime来说太复杂了,但是我在foreach之前调用了created_时间,所以它不是真正有效的。我就知道我应该早点去吃午饭!