将RSS pubDate转换为时间戳

hsz*_*hsz 12 php rss timestamp pubdate

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

Agu*_*nto 13

试试这个:

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


Geo*_*ams 10

您可以使用内置功能strtotime().它将日期字符串作为其第一个参数并返回Unix时间戳.

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

  • 当字符串包含时区时,看起来strtotime会给出奇怪的结果.例如,"星期六,2010年9月21日00:00 GMT"我得到1285372800(25-09-2010) (2认同)

Fel*_*Eve 7

strtotime 不适用于不同的时区.

我刚刚编写了这个函数来将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;
}
Run Code Online (Sandbox Code Playgroud)