JSON created_time转换preg_replace $字符串输入

问题描述:

这是一个很难描述。所以我使用Facebook图表在网站上显示我的网页facebook/twitter(通过Facebook)的视图。除了我试图让created_time变成更好的格式之外,一切都很棒。我测试过的CREATED_TIME的撅起嘴来确保它正常工作,像这样....

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

然而,当我尝试动态传递$数据 - > 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>"; 
    } 
} 
} 
?> 
+1

如果'$ string'是'2012-02-06T19:38: 35 + 0000',难道你不能跳过正则表达式并且执行'strtotime($ string)'? – Wiseguy 2012-02-06 22:34:04

+0

感谢您指出这一点,我有一些不必要的preg_replace-ing,因为我认为时间戳对于strtotime来说太复杂了,但我在foreach之前调用了created_time,所以它并不是真正有效的。我知道我早该去吃午饭了! – 2012-02-07 00:04:58

提示:当你为了挨饿排查东西不要推迟午餐。在午餐过后,你可以解决问题的可能性很大,因为你不需要挨饿,也不需要解决问题。答案比我想象的更简单!

的解决方案......

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

我试图解决当时叫之前,我有必要