tags:

views:

215

answers:

2

How do convert RSS feed publish date-time (GMT) to Unix timestamp using PHP?

I need to store the date into my table in a TIMESTAMP data type column.

+2  A: 

From MySQL Manual:

TIMESTAMP columns are displayed in the same format as DATETIME columns. In other words, the display width is fixed at 19 characters, and the format is 'YYYY-MM-DD HH:MM:SS'.

The RSS 2.0 specification states that:

All date-times in RSS conform to the Date and Time Specification of RFC 822, with the exception that the year may be expressed with two characters or four characters (four preferred).


So if we have the following RSS date:

$timeRSS = 'Sat, 07 Sep 2002 09:42:31 GMT'; // RFC 822

We need to do the following to convert it to MySQL TIMESTAMP format:

date_default_timezone_set('GMT'); // make sure we are using the same timezone
date('Y-m-d H:i:s', strtotime($timeRSS)); // 2002-09-07 09:42:31
Alix Axel
From what I remember, the "r" format in date() outputs RFC2822-compliant datetimes; might be slightly easier than specifying it longhand...
Matt Gibson
@Matt: `date('r')` = `Thu, 21 Dec 2000 16:01:07 +0200`, that's not compatible with MySQL `TIMESTAMP` format.
Alix Axel
@Alix -- Sorry, I was clearly reading before I'd drunk my coffee, and skipped a line or two.
Matt Gibson
+3  A: 

Would it be an option to use the PHP functions to generate a Unix Timestamp (ie seconds since Unix Epoch) and then let MySQL handle it from there?

PHP: - PHP Documentation

$timestamp = strtotime( 'Sat, 07 Sep 2002 09:42:31 GMT' ); // = 1031391751

MySQL: - MySQL Documentation

... `timestamp` = FROM_UNIXTIME( 1031391751 ) ...
Lucanos
No idea why this has less votes than Alix's answer - which does not address the question asked as well as this one.
symcbean