2010-05-26 19 views

Respuesta

10

Puede usar la función incorporada strtotime(). Toma una cadena de fecha como primer argumento y devuelve una marca de tiempo de Unix.

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

+2

Parece que strtotime dar resultados extraños cuando la cadena contiene la zona horaria. Por ejemplo, con "sáb, 21 sep 2010 00:00 GMT" obtengo 1285372800 (25-09-2010) –

12

intenta esto:

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

¡Excelente! Funciona como un amuleto – Borjante

7

strtotime no funciona con diferentes zonas horarias.

que acabo de escribir esta función para convertir pubDates RSS a las marcas de tiempo, que tiene en cuenta las diferentes zonas horarias:

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

Esto funcionó perfectamente. ¡Gracias! –

0

La función rsstotime() no funcionaba correctamente si el pubDate en rss feed tuvo zona horaria otra que +0000. El problema era con el modificador $, tenía que ser revertido. Para corregir esto dos líneas tenían que ser añadido, por lo que la línea:

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

se convirtió en:

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

sólo para aclarar la modificación - por ejemplo si el pubDate era Miér 22 May 2013 17:09:36 +0200 continuación, la fila

$timestamp = strtotime($strtotimearg, $timestamp 

offsetted el tiempo por dos horas no reseteado a +0000 zona horaria como se esperaba.

El Mié, 22 de mayo de 2013 17:09:36 +0200 muestra que la hora aquí presentada se encuentra en la zona horaria GMT +2.

El código no funcionaba correctamente y ha añadido extra de dos horas a la vez, por lo que el tiempo se convirtió en Miér 22 May 2013 19:09:36 +0000, en lugar Miér 22 May 2013 15:09:36 +0000 como debería haber sido.

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

Pruebe esta función

Cuestiones relacionadas