Come convertire una stringa di data Mon, 24 May 2010 17:54:00 GMT
dal feed RSS in un timestamp in PHP?Converti RSS pubDate in un timestamp
risposta
È possibile utilizzare la funzione incorporata strtotime()
. Prende una stringa di data come primo argomento e restituisce un timestamp Unix.
Try This:
$pubDate = $item->pubDate;
$pubDate = strftime("%Y-%m-%d %H:%M:%S", strtotime($pubDate));
Eccellente! Funziona come un fascino – Borjante
strtotime
non funziona con fusi orari diversi.
ho appena scritto questa funzione per convertire RSS pubDates al timestamp che non prendono in considerazione i 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;
}
Questo ha funzionato perfettamente. Grazie! –
La funzione rsstotime() non funzionava correttamente se il pubDate in rss feed aveva fuso orario altra di +0000. Il problema era con il modificatore $, doveva essere invertito. Per risolvere il problema che due linee dovevano essere aggiunti, quindi la linea:
$modifier = substr($timezone, 0, 1);
divennero:
$modifier = substr($timezone, 0, 1);
if($modifier == "+"){ $modifier = "-"; } else
if($modifier == "-"){ $modifier = "+"; }
Giusto per chiarire la modifica - ad esempio se il pubDate era Wed, 22 Maggio 2013 17:09:36 +0200 poi la riga
$timestamp = strtotime($strtotimearg, $timestamp
offsetted il tempo di due ore non resettato a +0000 timezone come previsto.
Il mer, 22 maggio 2013 17:09:36 +0200 indica che l'ora presentata qui è nel fuso orario GMT +2.
Il codice non funzionava correttamente e ha aggiunto in più di due ore alla volta, in modo che il tempo è diventato mer, 22 mag 2013 19:09:36 +0000, invece Mer 22 Mag 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
Sembra strtotime dare risultati strani quando la stringa contiene il fuso orario. Ad esempio con "Sat, 21 set 2010 00:00 GMT" Ottengo 1285372800 (25-09-2010) –