我有一个带有任意时区的ISO8601字符串。我不想转换为我的时区。相反,我想保留原始时区。我希望能够格式化输出,类似于 gmdate 格式化日期/时间的方式。例如:
$output = my_date_function( 'F d, ga', $thetime );
应输出:
$thetime = '2012-09-24T21:00:00-0700';
=> September 24, 9pm
$thetime = '2012-09-24T21:00:00-0500';
=> September 24, 9pm
我不知道
它是否有效,但我认为DateTime::createFromFormat可以自动完成。您还可以创建一个确定时区的函数,并使用它创建 DateTimezone 对象。http://php.net/manual/en/datetime.createfromformat.php根据文档:
如果省略时区且时间不包含时区,则将使用当前时区
法典:
$stime = '2012-09-24T21:00:00-0500';
$t = DateTime::createFromFormat(DateTime::ISO8601, substr($stime, 0, 19));
$output = $t->format('F d, ga');