2012-02-10 130 views
1

我试图提高从twitter XML文件拉入发布日期的可读性。结合正则表达式和strtotime

目前所显示的时间日期是2012-02-10T14:20:08Z

我申请一个正则表达式这一点,现在显示2012-02-10 14:20:08 GMT

现在我需要做的是将日期从美国格式转换为英国格式。我已经看过这个,看到它可以使用strtodate函数。但我不确定如何去做这件事。

   <strong>Tweets within 10 miles radius of London Eye</strong></br> 
       <?php 
       $feed = simplexml_load_file('http://search.twitter.com/search.atom?geocode=' . $feed1 . '%2C' . $range . '%22london%22&' . $lang . '&' . $amount); 
       if ($feed) { 
        foreach ($feed->entry as $item) { 

         $string = $item->published; 

//       $unixdate = strtotime($string); 
//       $ukDate = date('d/m/20y', $unixdate); 

         $find = array('/T/', '/Z/'); 
         $replace = array(' ', ' GMT'); 


         echo '<a href=\'' . $item->link->attributes()->href . '\'>' . $item->title . '</a>', '</br>' . preg_replace($find, $replace, $string), '</br>'; 
        } 
       } 
       else 
        echo "Cannot find Twitter feed!" 
        ?> 

是可以将这些功能组合在一起还是有更好的方法来做到这一点?

在此先感谢。

回答

3
foreach($feed->entry as $item) { 
    $date = date("d/M/Y g:iA T",strtotime($item->published)); 
    echo '<a href="' . $item->link->attributes()->href . '">' . $item->title . '</a></br>' . $date . '</br>'; 
} 

如果您想要更改它,请参阅date()格式的语法。请参阅strtotime()文档以了解其工作原理。

1

这样的事情应该这样做。只需将正则表达式的日期格式传递给strtotime函数即可。您可能还需要从正则日期中删除GMT字符串。

$date = preg_replace($find, $replace, $string); 
$formatted = date('d/m/Y', strtotime($date)); 

echo '<a href=\'' . $item->link->attributes()->href . '\'>' . $item->title . '</a>', '</br>' . $formatted, '</br>';