如何在PHP中将ISO8601日期转换为其他格式


How do I convert an ISO8601 date to another format in PHP?

Facebook以ISO8601格式输出日期,例如:2011-09-02T18:00:00

使用PHP,我如何重新格式化为如下内容:2011年9月2日星期五下午6:00

Nb-我是用Javascript做的,但IE有日期错误,所以我想要一个跨浏览器的解决方案。

一个快速但有时不可靠的解决方案:

$date = '2011-09-02T18:00:00';
$time = strtotime($date);
$fixed = date('l, F jS Y 'a't g:ia', $time); // 'a' and 't' are escaped as they are a format char.

设置此处详细说明的字符格式。

从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