简体   繁体   English

使用PHP中的原始时区格式化ISO-8601日期中的字符串

[英]Format string from ISO-8601 date with original timezone in PHP

I have a ISO8601 string with some arbitrary time zone. 我有一个带有任意时区的ISO8601字符串。 I don't want to convert to my time zone. 不想转换为我的时区。 Rather, I want to preserve the original time zone. 相反,我想保留原始时区。 I want to be able to format the output similar to how gmdate formats the date/time. 我希望能够格式化输出,类似于gmdate如何格式化日期/时间。 For example: 例如:

$output =  my_date_function( 'F d, ga', $thetime );

Should output: 应该输出:

$thetime = '2012-09-24T21:00:00-0700';
     => September 24, 9pm

$thetime = '2012-09-24T21:00:00-0500';
     => September 24, 9pm

I don't know if it would work but I think that DateTime::createFromFormat can do it automatically. 我不知道它是否可以工作,但我认为DateTime :: createFromFormat可以自动完成。 You can also create a function that determine the timezone and create a DateTimezone object with it. 您还可以创建一个确定时区的函数,并使用它创建一个DateTimezone对象。 http://php.net/manual/en/datetime.createfromformat.php According to the documentation : http://php.net/manual/en/datetime.createfromformat.php根据文档:

If timezone is omitted and time contains no timezone , the current timezone will be used 如果省略了时区并且time不包含时区 ,那么将使用当前时区

Code: 码:

$stime = '2012-09-24T21:00:00-0500';
$t = DateTime::createFromFormat(DateTime::ISO8601, substr($stime, 0, 19));
$output = $t->format('F d, ga');

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

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