【问题标题】:Combining regex and strtotime结合正则表达式和strtotime
【发布时间】:2012-03-03 03:16:03
【问题描述】:

我正在尝试提高从 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!"
                    ?>

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

提前致谢。

【问题讨论】:

    标签: php xml regex strtotime


    【解决方案1】:
    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() 文档以了解其工作原理。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-24
          • 1970-01-01
          • 1970-01-01
          • 2015-01-14
          • 2018-07-04
          相关资源
          最近更新 更多