将ISO 8601转换为unixtimestamp


Convert ISO 8601 to unixtimestamp

如何转换PHP中的2012-01-18T11:45:00+01:00(ISO 8601)到1326883500(unixtimestamp)?

此代码将ISO 8601日期时间转换为UTC的Unix时间戳。

echo date("U",strtotime('2012-01-18T11:45:00+01:00'));

较长版本:

$dateTime = new DateTime('2012-01-18T11:45:00+01:00');
$dateTime->setTimezone(new DateTimeZone('UTC'));
$utcTimestamp = $dateTime->getTimestamp();
echo $utcTimestamp;

要从ISO 8601转换为unixtimestamp:

strtotime('2012-01-18T11:45:00+01:00');
// Output : 1326883500

要从unixtimestamp转换为ISO 8601(时区服务器),请执行以下操作:

date_format(date_timestamp_set(new DateTime(), 1326883500), 'c');
// Output : 2012-01-18T11:45:00+01:00

要从unixtimestamp转换为ISO 8601(GMT):

date_format(date_create('@'. 1326883500), 'c') . "'n";
// Output : 2012-01-18T10:45:00+00:00

要从unixtimestamp转换为ISO 8601(自定义时区),请执行以下操作:

date_format(date_timestamp_set(new DateTime(), 1326883500)->setTimezone(new DateTimeZone('America/New_York')), 'c');
// Output : 2012-01-18T05:45:00-05:00