简体   繁体   English

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

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

Facebook outputs dates in ISO8601 format - eg: 2011-09-02T18:00:00 Facebook以ISO8601格式输出日期 - 例如:2011-09-02T18:00:00

Using PHP, how can I reformat into something like: Friday, September 2nd 2011 at 6:00pm 使用PHP,我如何重新格式化为:2011年9月2日星期五下午6点

Nb - I was doing it in Javascript, but IE has date bugs so I want a cross-browser solution. Nb - 我在Javascript中这样做,但IE有日期错误所以我想要一个跨浏览器的解决方案。

A fast but sometimes-unreliable solution: 一个快速但有时不可靠的解决方案:

$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.

Format characters detailed here . 格式字符在此处详述。

Converting from ISO 8601 to unixtimestamp : 从ISO 8601转换为unixtimestamp:

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

Converting from unixtimestamp to ISO 8601 (timezone server) : 从unixtimestamp转换为ISO 8601(时区服务器):

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

Converting from unixtimestamp to ISO 8601 (GMT) : 从unixtimestamp转换为ISO 8601(GMT):

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

Converting from unixtimestamp to ISO 8601 (custom timezone) : 从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

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

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