简体   繁体   English

RSS feed使用PHP将日期时间(GMT)发布为Unix时间戳格式

[英]RSS feed publish date-time (GMT) to Unix timestamp format using PHP

How do convert RSS feed publish date-time (GMT) to Unix timestamp using PHP? 如何使用PHP将RSS feed发布日期时间(GMT)转​​换为Unix时间戳?

I need to store the date into my table in a TIMESTAMP data type column. 我需要将日期存储在表中的TIMESTAMP数据类型列中。

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函数生成Unix时间戳(即距Unix纪元以来的秒数),然后让MySQL从那里进行处理?

PHP: - PHP Documentation PHP: -PHP文档

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

MySQL: - MySQL Documentation MySQL: -MySQL说明文件

... `timestamp` = FROM_UNIXTIME( 1031391751 ) ...

From MySQL Manual : MySQL手册

TIMESTAMP columns are displayed in the same format as DATETIME columns. TIMESTAMP列以与DATETIME列相同的格式显示。 In other words, the display width is fixed at 19 characters, and the format is 'YYYY-MM-DD HH:MM:SS'. 换句话说,显示宽度固定为19个字符,格式为“ YYYY-MM-DD HH:MM:SS”。

The RSS 2.0 specification states that: RSS 2.0规范指出:

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). RSS中的所有日期时间均符合RFC 822的日期和时间规范,但年份可以用两个字符或四个字符表示(四个优先)。


So if we have the following RSS date: 因此,如果我们具有以下RSS日期:

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

We need to do the following to convert it to MySQL TIMESTAMP format: 我们需要执行以下操作将其转换为MySQL TIMESTAMP格式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM