Domanda

Come convertire un Mon, 24 May 2010 17:54:00 GMT data stringa dal feed RSS per un timestamp in PHP?

È stato utile?

Soluzione

È possibile utilizzare la funzione di strtotime() built-in. Ci vuole una stringa data come primo argomento e restituisce un timestamp Unix.

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

Altri suggerimenti

Prova questo:

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

strtotime non funziona con fusi orari diversi.

Ho appena scritto questa funzione per convertire RSS pubDates al timestamp che non tengano conto dei diversi fusi orari:

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;
}

La funzione rsstotime () non funzionare correttamente se il pubDate in rss feed aveva fuso orario diverso da 0000 . Il problema era con il modificatore di $, doveva essere invertita. Per correzione che due linee dovevano essere aggiunti, quindi la linea:

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

è diventato:

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

Giusto per chiarire la modifica - ad esempio se il pubDate è stato Wed, 22 maggio 2013 17:09:36 +0200 , quindi la riga

$timestamp = strtotime($strtotimearg, $timestamp

offsetted il tempo di due ore non resettato a 0000 fuso orario come previsto.

Il Wed, 22 maggio 2013 17:09:36 +0200 mostra che il tempo qui presentata è nel fuso orario GMT +2.

Il codice non funzionava correttamente e ha aggiunto più di due ore per il tempo, in modo che il tempo è diventato Wed, 22 mag 2013 19:09:36 +0000 , anziché Wed, 22 maggio 2013 15:09:36 +0000 , come avrebbe dovuto essere.

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;  
}

Prova questa funzione

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top