Php 将任何日期字符串转换为不带时区的时间戳

Php 将任何日期字符串转换为不带时区的时间戳,php,sql,datetime,postgresql,Php,Sql,Datetime,Postgresql,我正在获取xml和rss提要,并将数据放入数据库。到目前为止,我遇到了两种不同的日期格式 Wed, 21 Jul 2010 00:28:50 GMT 及 我相信还会有更多。我的日期postgresql数据库是没有时区的时间戳。php中是否有现有函数,或者是否有将任意日期字符串转换为不带时区的时间戳(Y-m-d H:i:s)的过程?使用date: 结果: 2010-07-21 05:28:50 2010-07-20 22:33:19 结果: 2010-07-21 05:28:50 201

我正在获取xml和rss提要,并将数据放入数据库。到目前为止,我遇到了两种不同的日期格式

Wed, 21 Jul 2010 00:28:50 GMT


我相信还会有更多。我的日期postgresql数据库是没有时区的时间戳。php中是否有现有函数,或者是否有将任意日期字符串转换为不带时区的时间戳(Y-m-d H:i:s)的过程?

使用
date

结果:

2010-07-21 05:28:50
2010-07-20 22:33:19

结果:

2010-07-21 05:28:50
2010-07-20 22:33:19

时间戳被认为是UTC

$dt = new DateTime('Wed, 21 Jul 2010 00:28:50 GMT');
echo $dt->format('U'); // 1279672130
与相同的时间戳

$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 CEST');
echo $dt->format('U'); // 1279672130
请注意,
U
格式化选项需要PHP5.3。在日期字符串中提供时区标识符时,DateTime对象会识别时区,因此在GMT DateTime实例上调用以下命令时

echo $dt->format('Y-m-d H:i:s');
它将返回
2010-07-21 00:28:50
。您可以使用DateTime对象的
setTimezone()
方法更改其时区

$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 GMT+2');
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s'); // 2010-07-21 00:28:50

但是如果您只需要时间戳,就不需要了。

您根本不需要转换它。PostgreSQL应自动转换:

postgres=# create table test_tz (f1 timestamp without time zone);
CREATE TABLE
postgres=# insert into test_tz (f1) values ('Wed, 21 Jul 2010 00:28:50 GMT');
INSERT 0 1
postgres=# insert into test_tz (f1) values ('2010-07-20T17:33:19Z');
INSERT 0 1
postgres=# select f1 from test_tz;
         f1          
---------------------
 2010-07-21 00:28:50
 2010-07-20 17:33:19

有关Postgres认可的日期/时间格式的详细信息。
postgres=# create table test_tz (f1 timestamp without time zone);
CREATE TABLE
postgres=# insert into test_tz (f1) values ('Wed, 21 Jul 2010 00:28:50 GMT');
INSERT 0 1
postgres=# insert into test_tz (f1) values ('2010-07-20T17:33:19Z');
INSERT 0 1
postgres=# select f1 from test_tz;
         f1          
---------------------
 2010-07-21 00:28:50
 2010-07-20 17:33:19