JSON created_time 转换 preg_replace $string 输入

问题描述 投票:0回答:1

这是一个很难描述的问题。因此,我使用 Facebook 图表在网站上显示我的 facebook/twitter 页面(通过 Facebook)的视图。一切都很棒,除了我试图将created_time转换为更好的格式。我已经测试了created_time的噘嘴以确保它有效,就像这样......

$string = 2012-02-06T19:38:35+0000

但是,当我尝试动态传递 $data->created_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 json facebook facebook-graph-api preg-replace
1个回答
0
投票

提示:当您饥饿时,不要为了解决问题而推迟午餐。午餐后,当你不饿并且不那么迫切地解决问题时,你很可能会解决这个问题。答案比我想象的更简单!

解决方案...

<?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>";
    }
}   
}
?>

我试图在打电话之前确定时间,但没有必要

© www.soinside.com 2019 - 2024. All rights reserved.