繁体   English   中英

正则表达式和strtotime的结合

[英]Combining regex and strtotime

我正在尝试提高从Twitter XML文件提取发布日期的可读性。

当前显示的时间日期是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!"
                    ?>

是否可以将这些功能组合在一起,或者有更好的方法呢?

提前致谢。

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()文档以了解其工作方式。

这样的事情应该做。 只需将带有正则表达式的格式日期传递到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>';

不需要strtodate和regex-只需使用date()并以相关格式传递即可。 参见http://www.php.net/manual/en/function.date.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM