php 将 RSS pubDate 转换为时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2912262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:06:11  来源:igfitidea点击:

Convert RSS pubDate to a timestamp

phprsstimestamppubdate

提问by hsz

How to convert a date string Mon, 24 May 2010 17:54:00 GMTfrom RSS feed to a timestamp in PHP ?

如何将日期字符串Mon, 24 May 2010 17:54:00 GMT从 RSS 提要转换为PHP 中的时间戳?

采纳答案by Geoff Adams

You can use the built-in function strtotime(). It takes a date string as its first argument and returns a Unix timestamp.

您可以使用内置函数strtotime()。它接受一个日期字符串作为它的第一个参数并返回一个 Unix 时间戳。

http://php.net/manual/en/function.strtotime.php

http://php.net/manual/en/function.strtotime.php

回答by Agus Puryanto

Try This:

尝试这个:

$pubDate = $item->pubDate;
$pubDate = strftime("%Y-%m-%d %H:%M:%S", strtotime($pubDate));

回答by Felix Eve

strtotimedoes not work with different timezones.

strtotime不适用于不同的时区。

I have just written this function to convert RSS pubDates to timestamps which does take into account the different timezones:

我刚刚编写了这个函数来将 RSS pubDates 转换为时间戳,它确实考虑了不同的时区:

function rsstotime($rss_time) {
    $day       = substr($rss_time, 5, 2);
    $month     = substr($rss_time, 8, 3);
    $month     = date('m', strtotime("$month 1 2011"));
    $year      = substr($rss_time, 12, 4);
    $hour      = substr($rss_time, 17, 2);
    $min       = substr($rss_time, 20, 2);
    $second    = substr($rss_time, 23, 2);
    $timezone  = substr($rss_time, 26);

    $timestamp = mktime($hour, $min, $second, $month, $day, $year);

    date_default_timezone_set('UTC');

    if(is_numeric($timezone)) {
        $hours_mod    = $mins_mod = 0;
        $modifier     = substr($timezone, 0, 1);
        $hours_mod    = (int) substr($timezone, 1, 2);
        $mins_mod     = (int) substr($timezone, 3, 2);
        $hour_label   = $hours_mod > 1 ? 'hours' : 'hour';
        $strtotimearg = $modifier . $hours_mod . ' ' . $hour_label;
        if($mins_mod) {
            $mins_label = $mins_mod > 1 ? 'minutes' : 'minute';
            $strtotimearg .= ' ' . $mins_mod . ' ' . $mins_label;
        }
        $timestamp = strtotime($strtotimearg, $timestamp);
    }

    return $timestamp;
}

回答by Robert

The rsstotime() function did not work properly if the pubDate in rss feed had timezone other than +0000. The problem was with the $modifier, it had to be reversed. To fix that two lines had to be added, so the line:

如果 rss 提要中的 pubDate 的时区不是+0000,则 rsstotime() 函数无法正常工作。问题出在 $modifier 上,它必须颠倒过来。要解决该问题,必须添加两行,因此该行:

$modifier = substr($timezone, 0, 1); 

became:

变成了:

$modifier = substr($timezone, 0, 1);        
if($modifier == "+"){ $modifier = "-"; } else
if($modifier == "-"){ $modifier = "+"; }

Just to clarify the modification - for example if the pubDatewas Wed, 22 May 2013 17:09:36 +0200then the row

只是为了澄清修改 - 例如,如果pubDateWed, 22 May 2013 17:09:36 +0200那么该行

$timestamp = strtotime($strtotimearg, $timestamp

offsetted the time by two hours not resetted it to +0000timezone as expected.

将时间偏移了两个小时,但未按预期将其重置为+0000时区。

The Wed, 22 May 2013 17:09:36 +0200shows that the time presented here is in the timezone GMT +2.

星期三,2013 5月22日17时09分36秒+0200显示,这里给出的时间是在时区GMT +2。

The code did not work properly and added extra two hours to the time, so the time became Wed, 22 May 2013 19:09:36 +0000, instead Wed, 22 May 2013 15:09:36 +0000as it should have been.

代码无法正常工作并额外增加了两个小时的时间,所以时间变成了Wed, 22 May 2013 19:09:36 +0000,而不是Wed, 22 May 2013 15:09:36 +0000应该是.

回答by Davinder Singh

function pubdatetotime($pubDate) {

$months = array('Jan' => '01', 'Feb' => '02', 'Mar' => '03', 
'Apr' => '04', 'May' => '05', 'Jun' => '06', 
'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 
'Oct' => '10', 'Nov' => '11', 'Dec' => '12');

$date = substr($pubDate, 5,11);
$year = substr($date, 7,4); 
$month = substr($date, 3,3);
$d =  substr($date, 0,2);

$time = substr($pubDate, 17,8);

return $year."-".$months[$month]."-".$d." ".$time;  
}

Try this function

试试这个功能